microsoft / microsoft/TypeScript
Nested lookuptypes do not inference correctly
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
While trying to scope certain keys that would point to different types, I stumbled upon a problem when I needed to nest lookup types to lookup what type the key would correspond to. During decleration, this did not seem to be possible to get the compiler to understand. Applying the type does actually give out the correct type inferencing though. A simple example can be observed below.
TypeScript Version: 3.2.0-dev.201xxxxx
Tested with both 3.1.1 and typescript@next
Search Terms:
Lookuptypes, type inference, keyof
Code
interface Y {
x: {
x1: "x1";
x2: "x2";
}
}
interface Z {
x1: number;
x2: string;
x3: number;
}
type H<K extends keyof Y, J extends keyof Y[K]> = (k: K, j: J) => Z[Y[K][J]]; // <----- Error Incorrectly?!
type T6 = H<"x", "x1">;
type T7 = H<"x", "x2">;
type T8 = H<"x", "x3">; // Errors Correctly!
// @ts-ignore
type L<K extends keyof Y, J extends keyof Y[K]> = (k: K, j: J) => Z[Y[K][J]];
type T3 = L<"x", "x1">;
type T4 = L<"x", "x2">;
type T5 = L<"x", "x3">; // Errors Correctly!
Expected behavior:
For the Z[Y[K][J]] in the example to not give error. Applying the type seems to work correctly.
Actual behavior:
Decleration of the type shows error when it should not.
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 dem verlinkten TypeScript Playground-Beispiel und reproduziere die Diagnose beim verschachtelten indizierten Zugriff im Typ H. Verfolge die Verarbeitung des Compilers von Z[Y[K][J]] für generische Lookup-Typen; abgeschlossen ist die Aufgabe, wenn die Deklaration akzeptiert wird, während der ungültige Fall T8 weiterhin einen Fehler meldet.
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