microsoft / microsoft/TypeScript
Deprecate homomorphic mapped type over primitive
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
homomorphic mapped primitive
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
I would like homomorphic mapped types over primitives to emit a warning if the base type is not an object type. An explicit conditional type Foo<T> = T extends object ? { [K in keyof T]: ...} : T can be used to recover the pre-existing behavior. This should be mentioned in the warning message.
Homomorphic mapped type over primitives has surprising behavior that bears mention in the FAQ:
- This mapped type returns a primitive type, not an object type.
- Mapped types declared as
{ [ K in keyof T ]: U }whereTis a type parameter are known as homomorphic mapped types, which means that the mapped type is a structure preserving function of T. When type parameterT` is instantiated with a primitive type the mapped type evaluates to the same primitive.
This behavior is surprising since:
- It is not referentially transparent; that is, replacing a type parameter with its value does not result in the same type
- Although a mapped type may be assignable to the original type, it may specifically not be, e.g.
{[k in keyof T]: never}whereas it always is in the case of primitives. - The expression
keyof Thas surprising behavior on primitive types, differing substantially from object types. - The syntax of a mapped type visually suggests an object-like type, which is incorrect when mapping over
null.
📃 Motivating Example
type NullValued<X> = {[k in keyof X]: null};
type Okay = NullValued<{x:1, y:2}>; // {x:null, y:null}
type Oops = NullValued<number>; // oops! This is `number`, which is likely not what you expected
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- You have to know about homeomorphic mapped types and anticipate this corner case.
- What workarounds are you using in the meantime?
- A type guard
type Foo<T extends object> = { [K in keyof T]: ...}guards against the current behavior. - A conditional
type Foo<T> = T extends object ? { [K in keyof T]: ...} : Tmakes the current behavior explicit.
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 motivador NullValued y el pasaje de FAQ del issue; no se menciona ningún archivo de implementación ni ninguna prueba. Se considera terminado cuando los homomorphic mapped types sobre primitive bases producen la advertencia solicitada y la advertencia explica el workaround condicional explícito, mientras que el comportamiento sorprendente queda documentado en el FAQ.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100