Skip to content

TypeConfusion between Object and Array when destructuring an Array #57048

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

Closed
tobiasdcl opened this issue Jan 13, 2024 · 2 comments
Closed

TypeConfusion between Object and Array when destructuring an Array #57048

tobiasdcl opened this issue Jan 13, 2024 · 2 comments

Comments

@tobiasdcl
Copy link

πŸ”Ž Search Terms

spread, destructuring, { ...[]}

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Destructuring

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3#code/MYewdgzgLgBATgUwIYBsCCc5IJ4C4ZgCuAtgEYJwDaAujALwyUCMANDAExsDM1A3AFChIsAGZIA1ggxY8BEuSq0GAbxgA6DYlTScMAL4CA9IZimz5i5bMA9GAEkYAdxCEUAExgIAHgAcEwWAAVAGUYKBAYUGIfFCQASzAYJFIXWCgACziIGFJCURA4GAgQYgR4ZGLEuNg3EARssBAofkFwYpQENRQQAHMACgADACEkCAQPcDDsPwhgODifNOn6mAByLXRMHFWYLKTEpC3sHLzd7Ory1BRsAH4YABJlHWw1LOe+jeeASj0Br4EhO1Ot1+sNRuMYJMoMtZvNFlMZmsxJJnjs9kgDkcTrA9hcNtc7o9nq8IO9kVIjj8-gJ+J8jmofCAfH0vjBjE4CuIIPxycTGczWezALwbgFKdmC8+n8s4EJpJcWEMABOKTIA

πŸ’» Code

const realArray: number[] = [1, 2, 3];
const fakeArray: number[] = { ...realArray };
//                        ^ I would expect TS to complain about this but for some reason it does not

console.log(`Based on typescript types 'realArray' is an array but is it really? ${Array.isArray(realArray)}`);
console.log(`Based on typescript types 'fakeArray' is an array but is it really? ${Array.isArray(fakeArray)}`);

realArray.pop() // works
fakeArray.pop() // πŸ’₯ fakeArray.pop is not a function 

πŸ™ Actual behavior

there is no TypeError when assigning { ...[]} to a variable that is supposed to be an array based on the TS type

πŸ™‚ Expected behavior

I would expect a type error like Type '{}' is missing the following properties from type 'number[]': length, pop, push, concat, and 26 more. which happens when you try to assign { } to a variable with an Array type

Additional information about the issue

I hope this is no duplicate or that I am missing something obvious but I was not able to find something related to this specific issue

@MartinJohns
Copy link
Contributor

Duplicate of #34780. Used search terms: destructure object array in:title

@tobiasdcl
Copy link
Author

Duplicate of #34780. Used search terms: destructure object array in:title

Sorry, my bad!
Thanks πŸ™Œ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants