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

Object spread does not respect optional undefined union when strictOptionalProperties is on #44438

Closed
HerringtonDarkholme opened this issue Jun 4, 2021 · 0 comments · Fixed by #44696
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Jun 4, 2021

Bug Report

Another issue elated to #44421. This one seems to be more problematic due to runtime unsoundness.

🔎 Search Terms

strict optional properties, object spread, undefined

🕗 Version & Regression Information

Nightly, with strictOptionalProperties on

  • I was unable to test this on prior versions because strictOptionalProperties is a new option

⏯ Playground Link

Playground link with relevant code

💻 Code

var a: {a: string} = {a: ''}
var b: {a?: string} = {}
var c: {a: string | undefined} = {a: undefined}
var d: {a?: string | undefined} = {a: undefined}

var a1 = {a: 123, ...a} // error: "a" specified more than once
var b1 = {a: 123, ...b} // {a: number|string}
var c1 = {a: 123, ...c} // expect error "a" specified more than once
var d1 = {a: 123, ...d} // expect {a: string|number|undefined}

d1.a.toString() // crash! should report error

🙁 Actual behavior

c1 created without duplicate property error and d1 is inferred as {a: string | number}

🙂 Expected behavior

TS should report error on c1 and d1 should be inferred as {a: string|number|undefined} to prevent runtime crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants