microsoft / microsoft/TypeScript
Nested lookuptypes do not inference correctly
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio collegato di TypeScript Playground e riproduci la diagnostica sull’accesso indicizzato annidato nel tipo H. Traccia la gestione da parte del compilatore di Z[Y[K][J]] per i tipi di lookup generici; il lavoro è completo quando la dichiarazione viene accettata, mentre il caso non valido T8 continua a segnalare un errore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100