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

Length not checked for spread operator in params #26772

Closed
JacobEvelyn opened this issue Aug 30, 2018 · 3 comments
Closed

Length not checked for spread operator in params #26772

JacobEvelyn opened this issue Aug 30, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@JacobEvelyn
Copy link

TypeScript Version: 3.1.0-dev.20180830

Search Terms:
Spread length, Expected at least 1 arguments, but got 0 or more

Code

Porting this StackOverflow JS function into TypeScript, I got:

function *cartesian<T>(head: T[], ...tail: T[]): any {
  const remainder = tail.length > 0 ? cartesian(...tail) : [[]];
  for (let r of remainder) for (let h of head) yield [h, ...r];
}

Expected behavior:

The code compiles and works.

Actual behavior:

Expected at least 1 arguments, but got 0 or more.

Playground Link: http://www.typescriptlang.org/play/#src=function%20*cartesian%3CT%3E(head%3A%20T%5B%5D%2C%20...tail%3A%20T%5B%5D)%3A%20any%20%7B%0D%0A%20%20const%20remainder%20%3D%20tail.length%20%3E%200%20%3F%20cartesian(...tail)%20%3A%20%5B%5B%5D%5D%3B%0D%0A%20%20for%20(let%20r%20of%20remainder)%20for%20(let%20h%20of%20head)%20yield%20%5Bh%2C%20...r%5D%3B%0D%0A%7D

Related Issues:

I thought this might be #26350, but that seems slightly different. Various searches led me to #19419 and #4130, but those issues have been marked as fixed and closed.

@RyanCavanaugh
Copy link
Member

We don't have the "range" types that would be needed to turn the length check into a resulting type narrowing. See #15480

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 30, 2018
@JacobEvelyn
Copy link
Author

Ah interesting, I didn't realize "range" types would be required for something like this. Thanks for the info.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants