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
Expected behavior:
It's possible to use enum values as keys in interfaces which then are combined in unions.
Actual behavior:
Error shows:
Argument of type '{ a: klad; b: number; }' is not assignable to parameter of type 'combined'.
Type '{ a: klad; b: number; }' is not assignable to type 'two'.
Types of property 'a' are incompatible.
Type 'klad' is not assignable to type 'klad.bd'.(2345)
fixes it. The problem is that values inside of an object literal are automatically widened, so .a ends up being of type klad rather than the literal type klad.ad. TS is then unable to discriminate between the union members. Adding as const prevents the widening.
FWIW, this issue occurs even if you use string or number literals as the discriminant; it's not specific to enums.
Another workaround is to take advantage of contextual typing to prevent the widening by passing the object literal directly:
fn({a: klad.ad,b: 3});
or include a type annotation which invokes contextual typing in the same way:
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
deser
changed the title
Using enum values as property values in interfaces causes issue with union types
Using enum values as property values in interfaces causes issue with union types bug
Nov 4, 2019
Possibly this is a duplicate of #33243 but I'm not sure.
Sorry, if this is a duplicate.
TypeScript Version: 3.7-Beta
Search Terms: union enum value
Code
Expected behavior:
It's possible to use enum values as keys in interfaces which then are combined in unions.
Actual behavior:
Error shows:
Playground Link: http://www.typescriptlang.org/play/?ts=3.7-Beta#code/KYOwrgtgBA1gNgQwCZQN4CgpasqBeKAcmUIBpNsAjFAw6wqdAX3XQEsQAXYAJwDMEAY2BQA9iBEZsOAFyxESAHTJy0ygH454CJV6rsgzVEqjRcYAhDkW7LrwHConAO6i0FLAjnxki6vqwNORMzCysPKCQjbV0eZlZOAE8ABxFBUR0OYBoxCSgAHydXAG5WdJAAZ04oPhB8KAAKSjl0zIkkAEo5ADdRNhoAPjQoG3KqqG6EOHrUWXlfFWM5AGYR0vRahsm4DuKgA
Related Issues: Maybe #33243
The text was updated successfully, but these errors were encountered: