microsoft / microsoft/TypeScript
Support locally scoped type alias nodes
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
We mentioned this at a previous design meeting while discussing conditional types but I don't think an issue was opened for it (nor can I find mention of it within some design meeting notes). We think it'd be useful for both conditional types (to make a bare type reference/parameter) and in complex types (to reduce duplication) to enable a kind of type-alias-as-a-type-node syntax. Something that allows rewriting code like this:
type Foo<T, K extends string, TVal extends T[K] = T[K]> = TVal extends string ? {x: TVal} : never;
as
type Foo<T, K extends string> = type TVal = T[K] in TVal extends string ? {x: TVal} : never;
or
type MyBox<T, K extends keyof T = keyof T, TVal extends T[K] = T[K]> = {
host: T,
getValue(k: K): TVal,
setValue(k: K, v: TVal): TVal
};
as
type MyBox<T> = type K = keyof T, TVal = T[K] in {
host: T,
getValue(k: K): TVal,
setValue(k: K, v: TVal): TVal
};
this allows elision of unnecessary information (no need to write both a constraint and identical default), and prevents usages from accidentally providing an extra type parameter when they shouldn't (because the defaulted parameter was used effectively as a const). I believe @bterlson had mentioned wanting something like this in-person, too.
As for proposed syntax, I'd say:
- A
LocalTypeAliasis aTypeNode(so is valid anywhere aTypeNodeis). - A
LocalTypeAliasis parsed as a requiredtypekeword, then a comma separated list ofLocalTypeAliasAssignments (with at least one element) - the type assignment list, followed byinand an arbitraryTypeNode- the subject of the assignments. - A
LocalTypeAliasAssignmentis anIdentifierfollowed by=followed by aTypeNode. (does this need to be restricted to parse in a human-understandable way?)
For semantics:
- A
LocalTypeAliasis a local scope around its subject type node. It binds a declaration for a type parameter for each identifier in each assignment in its type alias assignment list, constrained to the assigned value, and establishes aTypeMapperto map those type parameters to their assigned value as well (similar to an instantiated generic type alias). (Should they be allowed to be mutually referential? Other parameter lists are, so I don't see why not.) These aliases are made to be type parameters (and not raw aliases like a nongeneric type alias declaration) so they interact favorably with conditional types - eg, they are a way to force a conditional type to iterate over a union for any type node without introducing another top-level alias.
Thoughts?
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 revisando la sintaxis y la semántica propuestas para LocalTypeAlias en el issue, incluido el parsing, el scoping, los parámetros de tipo y el comportamiento de TypeMapper. El payload no indica archivos de implementación ni tests, así que localiza los puntos de entrada del parser y del comprobador de tipos antes de continuar. Se considera terminado cuando haya acuerdo sobre el diseño y los ejemplos sean compatibles, sin preguntas sin resolver sobre referencias mutuas o restricciones de parsing.
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 28/100