Skip to content

Spreading an object causes target type to have properties from prototype chain while in runtime it does notΒ #62535

@msand

Description

@msand

πŸ”Ž Search Terms

"spread interface prototype chain"
"spread native classes includes properties from prototype chain"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about spreading objects and properties from prototype chain

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAhjAvDMBTA7jAsnADgHmgCcBLMAcwBoZizyA+ACgEoBuAKHdElgCMkYAbwB0ouAF8O7AGYBXMMCglwMYIwC2eAFzY8hKKQrVaFesyHsYq8BBAAbVMLshyGvMIDWqAJ4QWbdnFONV42IA

πŸ’» Code

const a = new Map<string, string>();

const b = {...a};

function c(map: Map<string, string>) {
  console.log(map.keys());
}

c(b);

πŸ™ Actual behavior

Typescript compiles without errors. It computes that the type of b satisfies the Map interface. Calling the function c with b as the only argument causes map.keys() to crash at runtime.

[ERR]: "Executed JavaScript Failed:" 
[ERR]: map.keys is not a function 

πŸ™‚ Expected behavior

Spreading into an object should only include own enumerable properties from the spread values in the type, nothing from the prototype chain.

Additional information about the issue

Is there any way to get the latest version of TypeScript to complain about the function call?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions