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

Shallow copy via object spread does not type check #16747

Closed
mmmichl opened this issue Jun 26, 2017 · 1 comment
Closed

Shallow copy via object spread does not type check #16747

mmmichl opened this issue Jun 26, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@mmmichl
Copy link

mmmichl commented Jun 26, 2017

TypeScript Version: 2.4.0 (playground)

Code

interface Car {
    doors: number;
}

const car = {
    doors: 4,
}

const carCopy: Car = {
    ...car,
    hp: 700,  // would expect a type error here
}

console.log(carCopy);  // {doors: 4, hp: 700}

Expected behavior:
When making a shallow copy of an object, where the resulting type is defined (via explicit typing, return type), I would expect unknown properties to be reported.

Actual behavior:
Compile error at mistyped/extraneous property.

This behaviour is consistent with Object.assign. Typescript will only complain about missing properties, but not of extraneous.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 26, 2017
@RyanCavanaugh
Copy link
Member

The playground is running 2.3. This is an error in 2.4. See #13878

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants