Skip to content

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

Closed
@HerringtonDarkholme

Description

@HerringtonDarkholme

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions