-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
π Search Terms
spread, destructuring, union
π Version & Regression Information
- This changed between versions 3.5.1 and 3.6.2. Specifically it changed with Fix destructuring with fallbackΒ #31711. It is still present in 4.2.3 and in nightly.
β― Playground Link
π» Code
interface A { a: string }
interface B { b: number }
function foo(x: A | B) {
const { a } = { ...x } // no error?!
// const a: string | undefined
a?.toUpperCase();
}
const x = { a: 1, b: 2 }
foo(x); // a.toUpperCase is not a function, oops
π Actual behavior
There is no error; a
is inferred to be of type string | undefined
.
π Expected behavior
I expected an error on destructuring assignment to a
saying Property 'a' does not exist on type 'A | B'
, as you would get with the seemingly equivalent construction without spreading
const { a } = x; // error! Property 'a' does not exist on type 'A | B'.
or the also-seemingly-equivalent construction with an intermediate variable
const v = { ...x }
const { a } = v; // error! Property 'a' does not exist on type '{ a: string; } | { b: number; }'
I figure this is a side effect of #31711. But is it considered a bug? a design limitation? or working as intended? Comes from this SO question.
Teneff
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue