microsoft / microsoft/TypeScript
Wrong overload selected on function when passed a callback annotated with a type with multiple optional properties
オープン
まだ誰も着手していません。
Bug
Domain: check: Type Inference
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
TypeScript Version: 2.7.0-dev.20171128
Code
This issue was first noticed in code that uses LoDash with the typings from DefinitelyTyped. It seems to manifest only in a very specific set of circumstances. The following code sample is as minimal a reproduction as I could find.
let _: LoDashStatic;
interface LoDashStatic {
mapValues<T extends object, TResult>(
obj: T,
callback: (value: T[keyof T]) => TResult
): { [P in keyof T]: TResult };
mapValues<T extends object>(obj: T, iteratee: object): { [P in keyof T]: boolean };
}
interface Item {
key?: string;
bar?: number;
}
interface Collection {
[key: string]: Item;
}
const source = { foo: { bar: 42 } };
let items: Collection = _.mapValues(
source,
(v: Item) => v
);
Note that the correct overload is selected when any of the following changes are made to the above code:
- Remove the type annotation on the callback:
(v) => vinstead of(v: Item) => v - Remove the nested optional property:
{ foo: {} }instead of{ foo: { bar: 42 } } - Remove the unused optional property:
interface Item { bar?: number; }instead ofinterface Item { key?: string; bar?: number; } - Annotate the source collection:
const source: Collection = { foo: { bar: 42 } };instead ofconst source = { foo: { bar: 42 } };
Expected behavior:
Compiles without issue.
Actual behavior:
error TS2322: Type '{ foo: boolean; }' is not assignable to type 'Collection'.
Property 'foo' is incompatible with index signature.
Type 'boolean' is not assignable to type 'Item'.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue 内の最小限の TypeScript 再現例から始め、注釈付き callback、オプショナルプロパティ、推論されたソース型に対するオーバーロード解決を調査します。サンプルが TS2322 エラーなしでコンパイルされ、object-iteratee オーバーロードではなく callback オーバーロードを選択すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100