microsoft / microsoft/TypeScript

Reusing function type with different parameters amount (overloading)

Offen
#45,153 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Variance Relationships
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

Is there any reason it's not possible to re-use a function type that is overloaded aka has different possible amount of arguments?

In the playground I put some code that shows this issue, but I will put a quick snippet here:

declare function foo(c: {a: number; b: string}): void
declare function foo(a: number, b: string): void


foo({ a: 1, b: "2" })

foo(1, "2")

// This doesn't work but is relatively understandable
const bar1: typeof foo = (a_c, b) => {
    a_c
    b
}

// This should work, why it doesn't?
const bar2: typeof foo = (...args) => {
    args
}

// it works fine, but you can't access the second parameter
const bar3: typeof foo = (a_c) => {  
    a_c
}

// it works fine, but you can't access the parameters
const bar4: typeof foo = () => {
}

bar2 in this example doesn't work, and the error that is given is Type '(args_0: number | { a: number; b: string; }, args_1: unknown) => void' is not assignable to type '{ (c: { a: number; b: string; }): void; (a: number, b: string): void; }'.

🔎 Search Terms

arguments overload parameter function

🕗 Version & Regression Information

In 4.4.0-beta & 4.3.5:

image

In 4.2.3 and before:

image

⏯ Playground Link

https://www.typescriptlang.org/play?target=99&jsx=0&ts=4.3.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVJxwAowAueAbynNWQFsAjEGAbngfIGcMYtUBzAL4BKcgDccWYAFgAUKEiwEKdNjwFi1eLUbMANOy48+-UfAlS5V2YkJEK8LQEYDHeACIATO-gjrt4hcPb2FrAHow+AAVAAssTnhgHBBOVAByDHgAdxwYAGt2ZEz4+DhobDEQCABPeDRQGG4oVGAoBggQOTA8bnZYJ3IMaoAHEBxEDXgAXngiKAB9MFdhaYA+Sjl4LcdFze2GOUFwyNiSzhicZAhgbNy8gyyY2qxMpJT0jAB+Lp7MhlhPIMRmMJgFprMAHRQ2D8TgrKbrCh7LYwziHY7wF63fIJRB8ECuIrwaqXeBgZoZRxgMApBIYGIITjgPA3YawKB0EAYZg-VC9f4wADMQNG40mMzmi3hiK2yJ2YHRsjkEUxmRyOKQ+MJmRJyDJFMyUGptPg9IQbJgHK5zDRsm6fL+sAALCKQeLZtKNrIjrIgA

🙁 Actual behavior

It just errors out when trying to use "...args" to get all the possible arguments of the function

🙂 Expected behavior

To be able to use "...args" or similar to get the arguments and typescript to not complain just for trying to use it

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

Beginne damit, das Verhalten bei der Zuweisung von Überladungen im verlinkten TypeScript Playground mit den gemeldeten Versionen 4.3.5 und 4.4.0-beta zu reproduzieren. Vergleiche die akzeptierten und abgelehnten Beispiele und ermittle anschließend das beabsichtigte Verhalten für Implementierungen überladener Funktionen mit Rest-Parametern; abgeschlossen ist die Aufgabe, wenn das Verhalten und die erwartete Typisierung festgestellt und durch einen geeigneten Compiler-Test abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
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.