microsoft / microsoft/TypeScript
Generic type not assignable if extends type with optional field and passed object does not include the optional field
オープン
まだ誰も着手していません。
In Discussion
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version: 3.9.2
Search Terms:
optional generic "not assignable"
Code
const foo = <V extends { s?: string }>(v: V) => {};
// This works:
foo({ s: "1", v: 2 });
// This doesn't work:
foo({ v: 2 }); // Argument of type '{ v: number; }' is not assignable to parameter of type '{ s?: string | undefined; }'. Object literal may only specify known properties, and 'v' does not exist in type '{ s?: string | undefined; }'.
Compare with
const bar = <V extends { }>(v: V) => {};
// Both work:
bar({ s: "1", v: 2 });
bar({ v: 2 });
Expected behavior:
Both statements in the first snippet should compile.
Actual behavior:
foo({ v: 2 }); doesn't compile for const foo = <V extends { s?: string }>(v: V) => {};
Related Issues:
Possible remotely related... https://github.com/microsoft/TypeScript/issues/13747
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されたPlaygroundリンクのTypeScript 3.9.2の再現コードを実行し、2つのジェネリック呼び出しを比較します。オプショナルフィールドを持つジェネリック制約に対する余分なプロパティの型チェックの挙動を追跡します。完了条件は、2つ目のスニペットで示されている挙動を変更せずに、1つ目のスニペットの両方の呼び出しがコンパイルできることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100