microsoft / microsoft/TypeScript
Reusing function type with different parameters amount (overloading)
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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:

In 4.2.3 and before:

⏯ Playground Link
🙁 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il comportamento di assegnazione degli overload nel TypeScript Playground collegato, usando le versioni riportate 4.3.5 e 4.4.0-beta. Confronta gli esempi accettati e rifiutati, quindi determina il comportamento previsto per le implementazioni con parametri rest di funzioni con overload; il lavoro è completo quando il comportamento e la tipizzazione prevista sono stati stabiliti e coperti da un test appropriato del compilatore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100