microsoft / microsoft/TypeScript

Wrong overload selected on function when passed a callback annotated with a type with multiple optional properties

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

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

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) => v instead 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 of interface Item { key?: string; bar?: number; }
  • Annotate the source collection: const source: Collection = { foo: { bar: 42 } }; instead of const 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'.

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

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

はじめの一歩

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

調査の方向性

Issue 内の最小限の TypeScript 再現例から始め、注釈付き callback、オプショナルプロパティ、推論されたソース型に対するオーバーロード解決を調査します。サンプルが TS2322 エラーなしでコンパイルされ、object-iteratee オーバーロードではなく callback オーバーロードを選択すれば完了です。

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

評価

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

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

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