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

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

Closed
dimak-dev opened this issue Jan 31, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@dimak-dev
Copy link

dimak-dev commented Jan 31, 2024

πŸ”Ž 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

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 31, 2024

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

@fatcerberus
Copy link

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)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 1, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants