microsoft / microsoft/TypeScript
Conditionally Mapped Type Leads to Confusing ts(2345) Error Message
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Reproduktion im Issue und den referenzierten Tests/Fällen in tests/cases/conformance/types/conditional/conditionalTypes2.ts um Zeile 227; vergleiche die Diagnosen in den aufgeführten TypeScript-Versionen und dem Playground-Link. Erledigt ist es, wenn der callB-Fehler keinen irreführenden conditional mapped type mehr präsentiert, während die erwartete ts(2345)-Diagnose erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100