microsoft / microsoft/TypeScript
Fail to infer Indexed access to the intersection type when it includes an union derived from a generic input
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔎 Search Terms
"indexed access on union", "generic index"
🕗 Version & Regression Information
5.6.2, and it seems never worked for every ts versions.
⏯ Playground Link
💻 Code
type Test<T extends string> = {[K in T]: {key: K}}[T] & {target: string}
const foo = <T extends string>(arg: Test<T>) => {
const value: Test<T> = arg;
const value2: Test<T>['key'] = arg['key'];
const value3: Test<T>['target'] = arg['target']; // Error
}
🙁 Actual behavior
The declaration of value and value2 works well, but ts emit an error saying Type 'string' is not assignable to type '{ [K in T]: { key: K; }; }[T]["target"] & string' for value3.
🙂 Expected behavior
The Test<T>['target'] should equal to string and assume the type of '{ [K in T]: { key: K; }; }[T]["target"] as any before intersection, as it does when I give non-generic type in the same place. It seems even weird because it well infers the type of Test<T>['key'].
Additional information about the issue
With additional experiment, I found that it always happens when I generate an union type with generic input. For instance,
type Test<T> = (T extends string ? {key: number} : {key: string}) & {target: string}
const foo = <T extends string>(arg: Test<T>) => {
const value: Test<T> = arg;
const value2: Test<T>['key'] = arg['key'];
const value3: Test<T>['target'] = arg['target']; // Error
}
it also does not work in the case above
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 la reproducción enlazada en TypeScript Playground y compara los casos de indexed access genérico y no genérico mostrados en el issue. Se considera terminado cuando el indexed access genérico para el tipo intersección acepta el valor string para la propiedad objetivo sin producir el error de tipo indicado, mientras la inferencia de claves existente sigue siendo correcta.
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
- 35/100