Bug Report
π Search Terms
spread, destructuring, union
π Version & Regression Information
β― Playground Link
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.
Bug Report
π Search Terms
spread, destructuring, union
π Version & Regression Information
β― Playground Link
Playground link
π» Code
π Actual behavior
There is no error;
ais inferred to be of typestring | undefined.π Expected behavior
I expected an error on destructuring assignment to
asayingProperty 'a' does not exist on type 'A | B', as you would get with the seemingly equivalent construction without spreadingor the also-seemingly-equivalent construction with an intermediate variable
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.