microsoft / microsoft/TypeScript

strictFunctionTypes prevents an assignment not related to functions

オープン
#28,671 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

TypeScript Version: 3.3.0-dev.20181122

Search Terms:
covariance, contravariance, keyof, interface, class, generic

Code

interface IBase {
    foo: string;
}

interface IDerived extends IBase {
    bar: string;
}

type StringPropertyNames<T> = { [P in keyof T]: T[P] extends string ? P : never }[keyof T]
type StringProperties<T> = Pick<T, StringPropertyNames<T>>

interface Foo<T> {
    readonly Bar: StringProperties<T>;
}

let baseProperties: StringProperties<IBase>
let derivedProperties: StringProperties<IDerived>

let baseInterface: Foo<IBase>
let derivedInterface: Foo<IDerived>

baseProperties = derivedProperties // no error
baseInterface = derivedInterface

tsc test.ts --strictFunctionTypes

Expected behavior:
Compiles without errors.

Actual behavior:

test.ts:23:1 - error TS2322: Type 'Foo<IDerived>' is not assignable to type 'Foo<IBase>'.
  Property 'bar' is missing in type 'IBase' but required in type 'IDerived'.

23 baseInterface = derivedInterface
   ~~~~~~~~~~~~~

  test.ts:6:5
    6     bar: string;
          ~~~
    'bar' is declared here.

Playground Link:

Related Issues:
#24190

The documentation says:

Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly.

I couldn't find any information saying it should affect anything else.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

test.ts にある提供された例を tsc test.ts --strictFunctionTypes で再現し、2 つの代入結果を比較してください。payload には実装ファイルも回帰テストも記載されていないため、報告されたジェネリックな mapped type の代入について、型チェックの経路を追跡してください。報告された代入の挙動が期待される結果と一致し、厳密な関数パラメーター検査に回帰が生じなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。