microsoft / microsoft/TypeScript

Reusing function type with different parameters amount (overloading)

Open
#45,153 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Variance Relationships
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:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.