You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript does not raise a type error when an object spread { ...array } is assigned to a variable of type number[]. This leads to an unintended assignment where an object is stored in a variable expected to be an array, causing runtime errors when array methods are used.
Reproduction
letfoo: number[]=[1,2];foo={ ...foo};// Expected a type error, but TypeScript allows thisconsole.log(foo);// Outputs: { "0": 1, "1": 2 } (Object, not an Array)console.log(Array.isArray(foo));// falsefoo.filter(num=>num===2);// Runtime Error: foo.filter is not a function
This issue is not limited to number[] and can be reproduced with other array types as well.
🕗 Version & Regression Information
This changed between versions ______ and _______
This changed in commit or PR _______
This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
I was unable to test this on prior versions because _______
⏯ Playground Link
No response
💻 Code
letfoo: number[]=[1,2];foo={ ...foo};// Expected a type error, but TypeScript allows thisconsole.log(foo);// Outputs: { "0": 1, "1": 2 } (Object, not an Array)console.log(Array.isArray(foo));// falsefoo.filter(num=>num===2);// Runtime Error: foo.filter is not a function
🙁 Actual behavior
letfoo: number[]=[1,2];foo={ ...foo};// Expected a type error, but TypeScript allows thisconsole.log(foo);// Outputs: { "0": 1, "1": 2 } (Object, not an Array)console.log(Array.isArray(foo));// falsefoo.filter(num=>num===2);// Runtime Error: foo.filter is not a function
🙂 Expected behavior
TypeScript should enforce stricter type checking in this scenario to prevent objects from being assigned to arrays unless explicitly cast.
Additional information about the issue
Would appreciate insights from the TypeScript team on whether this is intended behavior or a bug.
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
TypeScript does not raise a type error when an object spread { ...array } is assigned to a variable of type number[]. This leads to an unintended assignment where an object is stored in a variable expected to be an array, causing runtime errors when array methods are used.
Reproduction
This issue is not limited to number[] and can be reproduced with other array types as well.
🕗 Version & Regression Information
⏯ Playground Link
No response
💻 Code
🙁 Actual behavior
🙂 Expected behavior
TypeScript should enforce stricter type checking in this scenario to prevent objects from being assigned to arrays unless explicitly cast.
Additional information about the issue
Would appreciate insights from the TypeScript team on whether this is intended behavior or a bug.
The text was updated successfully, but these errors were encountered: