microsoft / microsoft/TypeScript

Feature Request: type for Arguments OF Function

Offen
#28,459 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

In der Issue werden keine Datei, kein Test und kein Compiler-Einstiegspunkt genannt. Beginne damit, das beabsichtigte Verhalten für überladene Funktionen, Tupelargumente und generische Restparameter zu klären, und füge anschließend Compilertests hinzu, die den vorgeschlagenen Typ ArgumentsOFFunction, gültige Aufrufe und die gezeigten Typfehler abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.