Skip to content

Type error when using spread operator of correct length #38195

@esromneb

Description

@esromneb

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions