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
{{ message }}
Repository navigation
Discriminated unions over enum members don't error as expected when used with number literals #12647
varzzz: A|B={x: 1,y: "hello"};// Errors as expected/*'Type '{ x: 1; y: string; }' is not assignable to type 'A | B'. Type '{ x: 1; y: string; }' is not assignable to type 'B'. Types of property 'y' are incompatible. Type 'string' is not assignable to type 'number'.'*/varzzz: A|B={x: 54,y: "hello"};// Errors as expected/*'Type '{ x: 54; y: string; }' is not assignable to type 'A | B'. Type '{ x: 54; y: string; }' is not assignable to type 'B'. Types of property 'x' are incompatible. Type '54' is not assignable to type '1'.'*/
changed the title [-]Discriminated unions over enum members don't error as expected when substituted with number literals[/-][+]Discriminated unions over enum members don't error as expected when used with number literals[/+]on Dec 4, 2016
I am seeing a related error in similar situation -
constenumkinds{first,second,}interfaceIFData{kind: kinds.first;data: string;}interfaceISData{kind: kinds.second;data: number;}typeData=IFData|ISData;functionchecker(input: Data){const{ kind, data }=input;switch(kind){/** * Property 'length' does not exist on type 'string | number'. * Property 'length' does not exist on type 'number'. * ^ Above error on following case line */casekinds.first: returndata.length;/** * Property 'length' does not exist on type 'string | number'. * Property 'length' does not exist on type 'number'. * ^ Above error on following case line */casekinds.second: returnMath.abs(data);}}
Discriminated unions with enum kinds do not seem to work as I expected. Can anyone suggest possible correction other than going back to literal string kinds ?
Activity
[-]Discriminated unions over enum members don't error as expected when substituted with number literals[/-][+]Discriminated unions over enum members don't error as expected when used with number literals[/+]normalser commentedon Dec 6, 2016
I think it's rather about "feature" of enums - #11559
sushruth commentedon Mar 24, 2019
I am seeing a related error in similar situation -
Discriminated unions with enum kinds do not seem to work as I expected. Can anyone suggest possible correction other than going back to literal string
kinds ?RyanCavanaugh commentedon Mar 26, 2019
Sushruth Sastry (@sushruth) narrowing doesn't work once the discriminant is picked off into its own variable via destructuring
sushruth commentedon Mar 28, 2019
Yeah, I kind of figured that. It would have been awesome if it did work.