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
interfaceOptionOne{kind: "one";ambiguousProperty: number;// This property name is shared, but has a different type in each unioned interfaceisThisABug: boolean;}interfaceOptionTwo{kind: "two";ambiguousProperty: string;// This property name is shared, but has a different type in each unioned interfaceprettySureThisIsABug: boolean;}typeOptionUnion=OptionOne|OptionTwo;// This function is riddled with errorsconstmyDeconstructedFunc=({kind, ambiguousProperty, ...otherParams}: OptionUnion)=>{if(kind==="one"){constmyLocalNum: number=ambiguousProperty;otherParams.isThisABug=true;console.log(myLocalNum);// So this thing is used}elseif(kind==="two"){constmyLocalString: string=ambiguousProperty;otherParams.prettySureThisIsABug=true;console.log(myLocalString);// So this thing is used}};// This function is error-freeconstmyNotDeconstructedFunc=(option: OptionUnion)=>{if(option.kind==="one"){constmyLocalNum: number=option.ambiguousProperty;option.isThisABug=true;console.log(myLocalNum);// So this thing is used}elseif(option.kind==="two"){constmyLocalString: string=option.ambiguousProperty;option.prettySureThisIsABug=true;console.log(myLocalString);// So this thing is used}};
Expected behavior:
Both of these functions have the same behavior (ideally both compiling without errors) Actual behavior: myDeconstructedFunc has compile errors and can't discern the correct type inside a union isolating condition. myNotDeconstructedFunc handles this case just fine. Playground Link: Link here
Related Issues:
I didn't find any that were specifically for deconstructing a shared property that was typed differently per unioned interface.
The text was updated successfully, but these errors were encountered:
Upon further investigation, this doesn't need the condition of "shared property with different types". This repros as soon as the discriminant is deconstructed. If the discriminant is readonly, I feel like TS should be smart enough to retain its intellisense in the deconstructed discriminant.
interfaceOptionOne{readonlykind: "one";foo: number;isThisABug: boolean;}interfaceOptionTwo{readonlykind: "two";bar: string;prettySureThisIsABug: boolean;}typeOptionUnion=OptionOne|OptionTwo;// riddled with errorsconstmyDeconstructedFunc=({kind, ...otherParams}: OptionUnion)=>{if(kind==="one"){constmyLocalNum: number=otherParams.foo;otherParams.isThisABug=true;console.log(myLocalNum);// So this thing is used}elseif(kind==="two"){constmyLocalString: string=otherParams.bar;otherParams.prettySureThisIsABug=true;console.log(myLocalString);// So this thing is used}};// no errorsconstmyNotDeconstructedFunc=(option: OptionUnion)=>{if(option.kind==="one"){constmyLocalNum: number=option.foo;option.isThisABug=true;console.log(myLocalNum);// So this thing is used}elseif(option.kind==="two"){constmyLocalString: string=option.bar;option.prettySureThisIsABug=true;console.log(myLocalString);// So this thing is used}};
TypeScript Version: 3.9.2
Search Terms:
discriminated union, discriminated
Code
Expected behavior:
Both of these functions have the same behavior (ideally both compiling without errors)
Actual behavior:
myDeconstructedFunc
has compile errors and can't discern the correct type inside a union isolating condition.myNotDeconstructedFunc
handles this case just fine.Playground Link:
Link here
Related Issues:
I didn't find any that were specifically for deconstructing a shared property that was typed differently per unioned interface.
The text was updated successfully, but these errors were encountered: