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

Passing spreading array to function parameters should raise an error #59390

Closed
zanminkian opened this issue Jul 22, 2024 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@zanminkian
Copy link

zanminkian commented Jul 22, 2024

πŸ”Ž Search Terms

spreading array function parameter

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about spreading array in function parameters.

⏯ Playground Link

No response

πŸ’» Code

function foo(args?: string): unknown {
  return args;
}
declare const arr: string[];

foo(...arr);

πŸ™ Actual behavior

No compilation error.

πŸ™‚ Expected behavior

TS compilation error when calling foo(...arr)

Additional information about the issue

One

function foo(): unknown
function foo(args: string): unknown
function foo(args?: string): unknown {
  return args;
}
declare const arr: string[];

foo(...arr); // ts compilation error here as expected πŸ€”οΈ

Two

Background

function getNames() {
  return ['foo', 'bar', 'foo'];
}

const uniqueNames = [...new Set(...getNames())]; // I mistakenly spreading the `getNames()`
console.log(uniqueNames) // [ 'f', 'o' ]

Once I've wrote code above. What I expect is [ 'foo', 'bar' ]. It's really hard to find out the problem in long-line codes, if TS not reports it in compilation time.

@MartinJohns
Copy link
Contributor

This is working as intended. See #48575 (comment):

The behavior we actually work hard to exhibit here is that a spread is valid if all its possible arities result in valid calls, [...]

I think your additional "One" does not work because overloads are not supported for this case.
And the "Two" version works unfortunately due to #52060 / #9726.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 22, 2024
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants