Skip to content

TypeConfusion between Object and Array when destructuring an ArrayΒ #57048

Closed
@tobiasdcl

Description

@tobiasdcl

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions