microsoft / microsoft/TypeScript

Add type operators that can answer "given function type T, what is the type of its return value when arguments P are passed in?"

Abierto
#40,179 8 comentarios 55 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

overload infer function operator arguments parameters return

Suggestion

// Already possible
type ValueForKey<T, K extends keyof T> = T[K];

// Proposal
type Callable = (...args: any[]) => any;
type ValueForArguments<T extends Callable, A extends paramsof T> = T(...A);

Use Cases

This is a proposal that would help with overloaded function issues like #26591 and would help my type testing library be able to support checks like expectTypeOf(fn).toBeCallableWith(args).

Note that though ValueForKey could be implemented as:

type ValueForKey<T, K extends keyof T> = 
  T extends {[Key in K]: infer U} ? U : never;

The following will only work for non-overloaded functions:

type ValueForArguments<T extends Callable, P extends Parameters<T>> = 
  T extends (...args: P) => infer R ? R : never;

since both the inference from Parameters<T> and the type definition will only consider one of the overloads.

What I'm asking is not for inference to consider multiple overloads, which would be nice but might complexify the implementation of type inference, but direct support for paramsof and T(A1, A2, ...A) type syntax which behaves correctly with overloaded functions.

Examples

interface OverloadedFunction {
  (arg: number): number;
  (arg: string): string;
}

// `number`
type Result1 = OverloadedFunction(number);

// `number | string`
type Result2 = OverloadedFunction(number | string);

// `[number] | [string]`
type Result3 = paramsof OverloadedFunction;

// `number | string`
type Result4 = OverloadedFunction(...paramsof OverloadedFunction)

Also

Given the above, it would also make sense to have something like

type Newable = new (...args: any[]) => any;
type ValueForArguments<T extends Newable, A extends paramsof new T> = new T(...A);

where new T takes the constructor type of T and turns it into a function type

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 especifica archivos de implementación, pruebas ni puntos de entrada. Empieza revisando la sintaxis de tipos propuesta y los ejemplos de funciones sobrecargadas; después, localiza las áreas del comprobador de tipos de TypeScript y las pruebas que rigen los operadores de tipos y la resolución de sobrecargas. Se considera terminado cuando los operadores propuestos admiten los ejemplos documentados sin cambiar la salida de JavaScript.

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.