Skip to content

Type checking is ignored with spread operator, especially with truthy expression.Β #57248

Closed as not planned

Description

@dimak-dev

πŸ”Ž Search Terms

spread operator with expression

πŸ•— Version & Regression Information

⏯ Playground Link

No response

πŸ’» Code

type Type = {
    property: string,
}

let typedVar1: Type = {
    property: 'string',
    anotherProperty: 'foo', // TS-2353: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'Type'.
}

let typedVar2: Type = {
    property: 'string',
    ...({anotherProperty: 'bar'}), // No error
}

let typedVar3: Type = {
    property: 'string',
    anotherProperty: 'foo', // TS-2783: 'anotherProperty' is specified more than once, so this usage will be overwritten.
    ...({anotherProperty: 'bar'}),
}

let typedVar4: Type = {
    property: 'string',
    anotherProperty: 'foo', // No error, total ingoring
    ...(1 == 1 && {anotherProperty: 'bar'}),
}

console.log(typedVar2); // {"property": "string", "anotherProperty": "bar"} 
console.log(typedVar2.anotherProperty); // TS-2339 : Property 'anotherProperty' does not exist on type 'Type'.
console.log(typedVar4); // {"property": "string", "anotherProperty": "bar"} 
console.log(typedVar4.anotherProperty); // TS-2339 : Property 'anotherProperty' does not exist on type 'Type'.

πŸ™ Actual behavior

  1. TS-2353: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'Type'
  2. No error
  3. TS-2783: 'anotherProperty' is specified more than once, so this usage will be overwritten.
  4. No error

πŸ™‚ Expected behavior

  1. TS-2353: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'Type'
  2. TS-2353: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'Type'
  3. TS-2353 + TS-2783
  4. TS-2353 + TS-2783

Additional information about the issue

No response

Activity

  1. MartinJohns commented on Jan 31, 2024

    @MartinJohns
    Contributor

    Duplicate of #39998. Type checking is not ignored and works, just excess property checks not.

  2. fatcerberus commented on Jan 31, 2024

    @fatcerberus

    le sigh... this error message really needs to be softened to make it more obvious EPC isn't a type check.
    Count this as another vote for something like #55152 (comment)

  3. typescript-bot commented on Feb 4, 2024

    @typescript-bot
    Contributor

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

  4. locked as resolved and limited conversation to collaborators on Oct 22, 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