Skip to content

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

Closed as not planned
@dimak-dev

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

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