microsoft / microsoft/TypeScript

Suggestion: allow `infer T` in type parameter constraint of `type` statement

Abierto
#39,736 2 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Search Terms

suggestion infer extends type parameter constraint

Suggestion

Allow infer T in the right of extends of a type statement.

Examples

This suggestion allows following code:

type ReturnType<T extends (...args: any) => infer R> = R;

Which behaves similarly as:

// this code is taken from lib.es5.d.ts
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

Use Cases

As demonstrated in the example, we sometimes use conditional types with the sole purpose of use of infer T.
We have to repeat some piece of code if we want to constraint what types can be passed to ReturnType.
So this suggestion provides an abbreviated syntax for this use case.

Open questions:

  • Should union distribution occur for the new syntax?
    • My opinion is yes to align with existing conditional types
    • hmm, but then just replacing any with infer _ would change the behavior...
  • Should function type parameters also allow infer T?
    • No strong opinion

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

No se nombran archivos fuente, pruebas ni puntos de entrada. Empieza revisando la sintaxis infer T propuesta y su comparación de tipos condicionales; después, resuelve las preguntas abiertas sobre la distribución de uniones y los parámetros de tipo de las funciones. Se considera terminado cuando la sintaxis y su semántica están especificadas con suficiente detalle para implementarlas y probarlas.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.