microsoft / microsoft/TypeScript

Conditionally Mapped Type Leads to Confusing ts(2345) Error Message

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

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

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

説明

TypeScript Version:
3.9.6
4.1.0-dev.20200914

Search Terms:
2345
conditional

Code

declare type GetPropertyNamesOfType<T, RestrictToType> = {
	[K in keyof T]: T[K] extends RestrictToType ? K : never;
}[keyof T];

You can find this gnarly type here:
https://github.com/microsoft/TypeScript/blob/94d4023043dd032a8c96a59677eb63526b6b24b6/tests/cases/conformance/types/conditional/conditionalTypes2.ts#L227

The following consuming/using code correctly generates ts(2345) errors for the last two lines:

interface A {
	req: string;
	num: number;
	str: string;
}
interface B {
	req: string;
	num?: number;
	str?: string;
}

declare function callA(name: GetPropertyNamesOfType<A, string>): void;
declare function callB(name: GetPropertyNamesOfType<Required<B>, string>): void;

const prop = "someKey";
callA(prop); // ts(2345)
callB(prop); // ts(2345)

Expected behavior:
In typescript@3.9.5 & typescript@3.8 (and a few earlier versions I tried) the error message for both lines is:

Argument of type '"someKey"' is not assignable to parameter of type '"req" | "str"'.

Actual behavior:
In typescript@^3.9.6 this is reported for both lines:

Argument of type '"someKey"' is not assignable to parameter of type 'GetPropertyNamesOfType<A, string>'.

That's correct (nice even!) for the callA() line, but it's confusing for the callB() line.

(This reproduction is a single file that can run into this problem... it's not how I ran into it. I had a ts.Program with many 10s of *.ts files in the same compilation and the message was referring to some type that made no sense in the source file that was giving me the error.)

Playground Link:
https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAXAngBwQcRIgCjA9ujCgHJQC2IAzgPIBmAKmiADwMA08AStYjAJZhEDfE3QA+eAF54AbwCwAKACQAbQDS8fgDt4AaxDJ8deAwC6ALlMaz8EAA9EIbcCrdeAoSLEIA-PE0rbRAANxAYAG4lAF9VAyMTcyjFJR0nGDooMAQAQTklZTgARysqPh0Ac2TlbQBXciD6gCNw6rKYUvLtKpjU7XTM7PgAIXyVYs6Bbuq68l9G8hbIgvb5+HbK5OilJVBIWAQ6Wu0hfnxdMCgICByACm0KECssXAIiUkfaRmYWHM4N7riACUVhC+H4wGSe2gcHgRxOiDOFyuEGG90ez2weEI4Q+lC+PhYPCKtX4cGALGG4n+XQqwNB4MhO0UYHOZXgqDe0ngACIqPhKOpDDzkpdrndOYQgRF4AB6WVIKi3ABMAGYACwAViBSjFqNuktQ0rlCsQSrVWp1iiAA

Related Issues:
I found reference to the gnarly type above in #32608 & #33568.
It looks to be the same "monster" as you see in #29505 too.

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

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

はじめの一歩

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

調査の方向性

issue の再現と、227 行付近にある参照先の tests/cases/conformance/types/conditional/conditionalTypes2.ts の tests/cases から始め、記載されている TypeScript バージョンと Playground のリンクで診断を比較します。callB のエラーが誤解を招く conditional mapped type を表示しなくなり、期待される ts(2345) の診断が維持されれば完了です。

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

評価

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

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

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