Skip to content

Using enum values as property values in interfaces causes issue with union types bug #34829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deser opened this issue Oct 30, 2019 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@deser
Copy link

deser commented Oct 30, 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

enum klad {
    ad = 'ad',
    bd = 'bd' 
}

interface one {
    a: klad.ad,
    b?: number,
    c?: boolean,
}

interface two {
    a: klad.bd,
    b?: boolean,
    d?: number
}

type combined = one | two;

const fn = (b: combined): void => { }

const val = { a: klad.ad, b: 3 };

fn(val);  // errors

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)

Playground Link: http://www.typescriptlang.org/play/?ts=3.7-Beta#code/KYOwrgtgBA1gNgQwCZQN4CgpasqBeKAcmUIBpNsAjFAw6wqdAX3XQEsQAXYAJwDMEAY2BQA9iBEZsOAFyxESAHTJy0ygH454CJV6rsgzVEqjRcYAhDkW7LrwHConAO6i0FLAjnxki6vqwNORMzCysPKCQjbV0eZlZOAE8ABxFBUR0OYBoxCSgAHydXAG5WdJAAZ04oPhB8KAAKSjl0zIkkAEo5ADdRNhoAPjQoG3KqqG6EOHrUWXlfFWM5AGYR0vRahsm4DuKgA

Related Issues: Maybe #33243

@fatcerberus
Copy link

fatcerberus commented Oct 30, 2019

Changing the assignment to val to:

const val = { a: klad.ad, b: 3 } as const;

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:

const val: combined = { a: klad.ad, b: 3 };

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 30, 2019
@typescript-bot
Copy link
Collaborator

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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants