microsoft / microsoft/TypeScript

Feature Request: type for Arguments OF Function

Aperta
#28,459 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Nell’issue non sono indicati file, test o punti di ingresso del compilatore. Inizia chiarendo il comportamento previsto per le funzioni sovraccaricate, gli argomenti tupla e i parametri rest generici, quindi aggiungi test del compilatore che coprano il tipo ArgumentsOFFunction proposto, le chiamate valide e gli errori di tipo mostrati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.