microsoft / microsoft/TypeScript

Feature Request: type for Arguments OF Function

Abierto
#28,459 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Search Terms

Suggestion

current typescript support ...args
but when we use

declare function a(x: number, y: number) : number
declare function a(x: number, s: string) : string

a(...[1, 2]) // => Error: TS2556: Expected 2 arguments, but got 0 or more.

will get error

Use Cases

Examples

declare function a(x: number, y: number) : number
declare function a(x: number, s: string) : string

Actual behavior:

a(...[1, 2]) // => Error: TS2556: Expected 2 arguments, but got 0 or more.
type ArgumentsOFFunction<T extends Function>

a type can let typescript know this is args array of target function

notice: this type is not exists

Expected behavior:

let args1: ArgumentsOFFunction<typeof a> = [x = 1, s = ''];
let args2: ArgumentsOFFunction<typeof a> = [x = 1, y = 2];
let args3: ArgumentsOFFunction<typeof a> = ['1', 2]; // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.

a(...args1) // => string
a(...args2) // => number

a(1, 'str') // => string
a(1, 2) // => number

a('1', 2) // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.

declare function b(...args: ArgumentsOFFunction<typeof a>) : boolean

b(...args1) // => boolean
b(...args2) // => boolean

b(1, 'str') // => boolean
b(1, 2) // => boolean

b('1', 2) // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.

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 ningún archivo, prueba ni punto de entrada del compilador. Empieza por aclarar el comportamiento previsto para las funciones sobrecargadas, los argumentos de tupla y los parámetros rest genéricos, y después añade pruebas del compilador que cubran el tipo ArgumentsOFFunction propuesto, las llamadas válidas y los errores de tipo mostrados.

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.