microsoft / microsoft/TypeScript
Mapped type of a conditional type unexpectedly fails inference
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
conditional type infer unknown
conditional type mapped type inference
🕗 Version & Regression Information
This is the behavior in every version I tried (3.3.3 to 5.5.3), and I reviewed the FAQ for entries about conditional types, mapped types, and structural inference.
⏯ Playground Link
💻 Code
type Mapped<T> = { [P in keyof T]: T[P] }
type ComponentA = Mapped<{ field: number }>
type ComponentB = { field: number }
type Infer1<T> = Mapped<T extends never ? null : T>
declare function infer1<T>(c: Infer1<T>): T
infer1({} as ComponentA).field = 1 // ✅
infer1({} as ComponentB).field = 1 // ❌ Error: Object is of type 'unknown'
type Infer2<T> = T extends never ? null : Mapped<T>
declare function infer2<T>(c: Infer2<T>): T
infer2({} as ComponentA).field = 1 // ✅
infer2({} as ComponentB).field = 1 // ✅
🙁 Actual behavior
infer1(ComponentA) passed inference but infer1(ComponentB) failed with unknown.
However, if it's the other way around — Conditional ? Mapped<T> instead of Mapped<Conditional ? T>, both infer2(ComponentA) and infer2(ComponentB) passed inference.
🙂 Expected behavior
I expect infer1(ComponentA) and infer1(ComponentB) to both work, because ComponentA and ComponentB are essentially the same type — I'm unable to otherwise differentiate them by any means.
Additional information about the issue
I discovered this while debugging vuejs/core#11353, and the interference failure there was eventually reduced to this report.
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 TypeScript Playground enlazado y el ejemplo reducido infer1/infer2; después, compara cómo las dos disposiciones de tipos condicionales y tipos mapeados infieren T para ComponentA y ComponentB. Se considera terminado cuando infer1 acepta ambas formas de componente equivalentes sin producir unknown, y el comportamiento está cubierto por una prueba de regresión adecuada.
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100