microsoft / microsoft/TypeScript
Reusing function type with different parameters amount (overloading)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the overload assignment behavior in the linked TypeScript Playground using the reported 4.3.5 and 4.4.0-beta versions. Compare the accepted and rejected examples, then determine the intended behavior for rest-parameter implementations of overloaded functions; done means the behavior and expected typing are established and covered by an appropriate compiler test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100