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.
TypeScript Version: 3.3.0-dev.20181129
Search Terms: spread array object
Code
Expected behavior:
{ ...x }should not be assignable tonumber[]Actual behavior:
{ ...x }is assignable tonumber[]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.