Conditionally Mapped Type Leads to Confusing ts(2345) Error Message
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 38/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- typescript
- 領域
- compilers
研究方向
從 issue 中的重現以及第 227 行附近所引用的 tests/cases/conformance/types/conditional/conditionalTypes2.ts 開始;比較列出的 TypeScript 版本和 Playground 連結中的診斷。完成的標準是,callB 錯誤不再呈現具有誤導性的 conditional mapped type,同時保留預期的 ts(2345) 診斷。
由索引模型根據 Issue 內容生成。
描述
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.)
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.
- 主要語言
- Go
- 星號
- 111k
- 分支
- 14.4k
- 平均合併
- 1 天 19 小時
- 30 天內合併 PR
- 117
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
microsoft/TypeScript 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 88/100
microsoft/TypeScript#64322 · 2 則留言 · 1 個 reaction · 已指派 2 人 ·
-
Possible Improvement
難度 2/5 1-3 小時 新手友好度 78/100
microsoft/TypeScript#64278 · 1 則留言 · 1 個 reaction ·
-
Docs
難度 2/5 1-3 小時 新手友好度 70/100
microsoft/TypeScript#64118 · 1 則留言 ·
-
難度 1/5 1 小時以內 新手友好度 88/100
microsoft/TypeScript#64094 ·
-
Docs
難度 2/5 1-3 小時 新手友好度 76/100
microsoft/TypeScript#63959 · 5 則留言 ·
查看 microsoft/TypeScript 的全部 Issue
相似的 Issue
-
optimization optimization:agents-md-curator
難度 2/5 1-3 小時 新手友好度 86/100
githubnext/gh-aw-cao#13143 ·
-
難度 2/5 1-3 小時 新手友好度 84/100
blinklabs-io/bursa#904 ·
-
難度 2/5 1-3 小時 新手友好度 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
難度 2/5 1-3 小時 新手友好度 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 則留言 ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist 未關閉bug
難度 2/5 1-3 小時 新手友好度 90/100