microsoft / microsoft/TypeScript

Allow use of infer in extends clause of generic type parameter

Abierto
#29,378 0 comentarios 9 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Search Terms

generic type parameter infer keyword conditional

Suggestion

Allow the use infer keyword in the extends clause of generic type parameters. This would simply be a nice quality of life improvement to avoid unnecessary conditional types or extra free type parameters.

type ArrayType<A extends Array<infer T>> = T;

Use Cases

Currently if you have a generic type whose parameter is constrained by a generic type, you need to write a conditional to extract the nested generic type:

type ArrayType<A extends Array<any>> = A extends Array<infer T> ? T : never;

This seems unfortunate that we need to write a conditional since only the true branch will ever be satisfied. (Additionally, avoiding unnecessary usages of any would be nice.) We could avoid some duplication by dropping the constraint:

type ArrayType<A> = A extends Array<infer T> ? T : never;

But passing a non array into this type would move the error to wherever ArrayType was used (or perhaps even be silenced) instead of on the type parameter itself where the error actually occurred.

We could also do this with an extra type parameter, but this puts the onus on the user of the type to pass a correct value (and they can always just pass unknown or any):

type ArrayType<A extends Array<T>, T> = T;
type T2 = ArrayType<number[], number>;
type T3 = ArrayType<number[], unknown>;
type T1 = ArrayType<number[], any>;

Examples

type MyType<A, B> = { ... }

type OldLift<T extends MyType<any, any>, B> = T extends MyType<infer A, any>
    ? MyType<A, B>
    : never;

type NewLift<T extends MyType<infer A, any>, B> = MyType<A, B>;

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue no proporciona ningún archivo, prueba ni punto de entrada; empieza estudiando los ejemplos genéricos propuestos y el comportamiento existente de los tipos condicionales. El trabajo estará terminado cuando infer se acepte en las cláusulas extends de los parámetros de tipo genéricos, se conserve el comportamiento de las restricciones indicado y haya cobertura para los casos ArrayType y OldLift/NewLift.

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
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.