Skip to content
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

Object Spread { ...array } Incorrectly Assignable to number[] #61295

Closed
nadavushka-dev opened this issue Feb 27, 2025 · 3 comments
Closed

Object Spread { ...array } Incorrectly Assignable to number[] #61295

nadavushka-dev opened this issue Feb 27, 2025 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@nadavushka-dev
Copy link

🔎 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

let foo: number[] = [1, 2];
foo = { ...foo }; // Expected a type error, but TypeScript allows this

console.log(foo); // Outputs: { "0": 1, "1": 2 } (Object, not an Array)
console.log(Array.isArray(foo)); // false

foo.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

let foo: number[] = [1, 2];
foo = { ...foo }; // Expected a type error, but TypeScript allows this

console.log(foo); // Outputs: { "0": 1, "1": 2 } (Object, not an Array)
console.log(Array.isArray(foo)); // false

foo.filter(num => num === 2); // Runtime Error: foo.filter is not a function

🙁 Actual behavior

let foo: number[] = [1, 2];
foo = { ...foo }; // Expected a type error, but TypeScript allows this

console.log(foo); // Outputs: { "0": 1, "1": 2 } (Object, not an Array)
console.log(Array.isArray(foo)); // false

foo.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.

@MartinJohns
Copy link
Contributor

Duplicate of #60063.

@jcalz
Copy link
Contributor

jcalz commented Feb 27, 2025

also #34780

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 27, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2025
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

5 participants