Skip to content

Type error when using spread operator of correct length #38195

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

Closed
esromneb opened this issue Apr 26, 2020 · 3 comments
Closed

Type error when using spread operator of correct length #38195

esromneb opened this issue Apr 26, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@esromneb
Copy link

I am trying to do a simple spread operator but I am getting typescript errors.

TypeScript Version: 3.8.3

Code

type Vec2 = [number,number];
type Vec4 = [number,number,number,number];

const a: Vec2  = [0,1];
const b: Vec2  = [2,3];
const broken: Vec4 = [...a,...b];
const works: Vec4 = [a[0],a[1],b[0],b[1]];
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

Expected behavior:
I expect this to work. I expect broken and works to both have the value of [0,1,2,3]

Actual behavior:
The broken line gives me this error:

[tsl] ERROR in /mnt/src/main.ts(6,7)
      TS2739: Type 'number[]' is missing the following properties from type '[number, number, number, number]': 0, 1, 2, 3

Playground Link:

Playground Link

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented Apr 27, 2020

The new issue template asks for search terms and related issues in order to help the team (which I'm not part of, just a nosy bystander here 🙋‍♂️👋) triage and respond to them.

This is likely a duplicate of #27859; tuples don't currently preserve their tuple-ness when spread.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 1, 2020
@RyanCavanaugh
Copy link
Member

This has been fixed; you can select "Nightly" in the version selector on the Playground to see that the error no longer occurs. Thanks!

@jcalz
Copy link
Contributor

jcalz commented May 1, 2020

Fixed in #36861 ? Or somewhere else?

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