Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spread error when using tuples within overload signatures #54812

Closed
Enteleform opened this issue Jun 28, 2023 · 5 comments
Closed

spread error when using tuples within overload signatures #54812

Enteleform opened this issue Jun 28, 2023 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Enteleform
Copy link

Bug Report

🔎 Search Terms

  • overload
  • spread

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = [number, number]
type B = [string, string]
type AB = A | B

function foo(...args:A): void /* works when removed */
function foo(...args:B): void /* works when removed */
function foo(...args:AB){}

const args = [1, 2] as AB

foo(...args)
// A spread argument must either have a tuple type or be passed to a rest parameter.(2556)

🙁 Actual behavior

Type Error:

A spread argument must either have a tuple type or be passed to a rest parameter.(2556)

🙂 Expected behavior

No type errors. All resolutions of the AB union are valid tuples and should be spreadable.

@jcalz
Copy link
Contributor

jcalz commented Jun 28, 2023

Essentially a duplicate of #14107

@mkantor
Copy link
Contributor

mkantor commented Jun 28, 2023

Even if this continues to be an error, the message is pretty confusing. "A spread argument must either have a tuple type or be passed to a rest parameter"… but args is a union of tuple types and it is being passed to a rest parameter.

(I guess pedantically-speaking a union of tuple types is not "a tuple type", and arguably a rest parameter with a fixed length isn't a "real" rest parameter. But removing the overload signatures makes this error go away, so neither of those things are really the problem here.)

I think ideally the error message would say something about ...args not satisfying any individual overload signature.

@fatcerberus
Copy link

arguably a rest parameter with a fixed length isn't a "real" rest parameter

Fun fact, this isn’t just a “no true Scotsman” quibble - IIRC TS sees no difference between (...args: [ number, number ]) and (arg1: number, arg2: number) at the type level. As far as the compiler is concerned there is no rest parameter, “real” or otherwise. This lets you use tuple types to express parameter lists generically.

That said, I’ve agreed for a while that the wording of this error message is confusing in many of the cases where it shows up.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 28, 2023
@RyanCavanaugh
Copy link
Member

Agreed; if someone wants to update the error message that'd be fine

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants