Skip to content

Spreading array into object should not be assignable to array #28801

@AnyhowStep

Description

@AnyhowStep

TypeScript Version: 3.3.0-dev.20181129

Search Terms: spread array object

Code

const x: number[] = [1, 2, 3, 4];
//Oh, look, a typo.
//Really meant [...x]
//But used     {...x}
const y: number[] = { ...x };

//undefined
console.log(y.length);
//Uncaught TypeError: y.indexOf is not a function
console.log(y.indexOf(3));

Expected behavior:

{ ...x } should not be assignable to number[]

Actual behavior:

{ ...x } is assignable to number[] and results in run-time errors.

Playground Link: here


I actually ran into this problem because I was refactoring some code and decided to use arrays instead of objects. However, I missed a few spots. Luckily, I had unit tests to scream at me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions