You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
interfaceA{a: string}interfaceB{b: number}functionfoo(x: A|B){const{ a }={ ...x}// no error?! // const a: string | undefineda?.toUpperCase();}constx={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
constv={ ...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.
The text was updated successfully, but these errors were encountered:
Bug Report
π Search Terms
spread, destructuring, union
π Version & Regression Information
β― Playground Link
Playground link
π» Code
π Actual behavior
There is no error;
a
is inferred to be of typestring | undefined
.π Expected behavior
I expected an error on destructuring assignment to
a
sayingProperty '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.
The text was updated successfully, but these errors were encountered: