Skip to content

no error when using spread operator with object of incorrect type #41698

Closed

Description

@DetachHead

TypeScript Version: 4.2.0-dev.20201126

Search Terms:
spread operator type safe
Code

type Foo = {bar: number}
let foo: Foo = {bar: 1}

foo.baz = 1 //Property 'baz' does not exist on type 'Foo'.
foo = {...foo, baz: 1 } //Type '{ baz: number; bar: number; }' is not assignable to type 'Foo'. Object literal may only specify known properties, and 'baz' does not exist in type 'Foo'.
foo = {...foo, ...{baz: 1} } // no error

Expected behavior:
error on the last line as property baz does not exist on type Foo
Actual behavior:
no error
Playground Link:
https://www.typescriptlang.org/play?ts=4.2.0-dev.20201126#code/C4TwDgpgBAYg9nKBeKBvARgQwE4C4oB2ArgLboTYC+AsAFAA2EwUAZgvvIihjvgIw1adNnAB0WAF7IofKAHo5ABWxxI2UFADkkzVAAmcCAGdCcZhAAeASyPM4BKKEhbOm0cITTUonyIA0UJL8UJTycgAq4NCaqIGYEvjEZBQA3HF4hKTk2GmUujamzJhGRlYA5gSY6IyOiE7RrqJQAPLoAFYQAMbM9FbAFJj0UCSYIFD29GNGkJ1WLGMA1gRwAO4OYCpqwFbGAZgEelo6+oYmy+bWtlBWDvUuCG4eXGg+ov5QrzwJMqGhCqZQCgqbBAA
Related Issues:

Activity

  1. MartinJohns commented on Nov 26, 2020

    @MartinJohns
    Contributor

    Duplicate of #39998.

    On a side-note: This is still type-safe. Extra-properties on objects are allowed. Excess property checks are more of a linter feature.

  2. typescript-bot commented on Dec 5, 2020

    @typescript-bot
    Contributor

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

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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