1

I'm looking for a way to create a Typescript type from ENUM values. Currently I'm having to manually construct the type and keep it in sync with the ENUM.

Is there a way to create a type as a oneof from ENUM values? End result being when I update the ENUM the type will automatically reflect it

enum FRUITS {
    APPLE = 'apple',
    PEAR = 'pear',
    ORANGE = 'orange',
}

// type Fruits = FRUITS;
type Fruits = 'apple' | 'pear' | 'orange';

// This works
const fruit: Fruits = FRUITS.APPLE;
// This also works
const fruit2: Fruits = 'apple';

The type will be used in these scenarios so both have to still work:

const fruit: Fruits = FRUITS.APPLE;

const fruit2: Fruits = 'apple';

1 Answer 1

6

I always recommend staying away from enum unless the values are truly opaque, meaning you do not need any TS code outside the enum declaration to refer to the number/string as literal values. (Actually I tend to stay away from numeric enum entirely, since all number values are assignable to them, see microsoft/TypeScript#17734 among other issues)

For the specific use cases presented in your example, I'd be inclined to drop enum and use a strongly-typed enumlike object instead:

const FRUITS = {
  APPLE: 'apple',
  PEAR: 'pear',
  ORANGE: 'orange',
} as const;

That allows Fruits to be defined programmatically:

type Fruits = typeof FRUITS[keyof typeof FRUITS];

And then everything in your example still works:

const fruit: Fruits = FRUITS.APPLE;
const fruit2: Fruits = 'apple';

Playground link to code

Sign up to request clarification or add additional context in comments.

1 Comment

That's brilliant. I had tried this approach before but without the as const. Without this it turns the type into a generic string which wasn't what I was after! Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.