microsoft / microsoft/TypeScript
Nested lookuptypes do not inference correctly
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el ejemplo enlazado de TypeScript Playground y reproduce el diagnóstico sobre el acceso indexado anidado en el tipo H. Rastrea el manejo por parte del compilador de Z[Y[K][J]] para tipos de búsqueda genéricos; el trabajo está terminado cuando se acepta la declaración, mientras que el caso no válido T8 sigue notificando un error.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100