138

I'd like to get a string literal union from an enum.

For this enum…

enum Weekday {
    MONDAY = 'mon',
    TUESDAY = 'tue',
    WEDNESDAY = 'wed'
}

… I'd like to get this:

type WeekdayType = 'mon' | 'tue' | 'wed';

I tried typeof keyof Weekday but that resulted in 'MONDAY' | 'TUESDAY' | 'WEDNESDAY'. Feel like the solution might have to do with mapped types but I can't seem to wrap my head around it.

How do I do this?

AKG
  • 2,936
  • 5
  • 27
  • 36
  • That's unfortunate! – AKG Sep 19 '18 at 01:18
  • @jcalz when people 'answer' in comments like you did, the answers cannot be edited by the community. It's a pity, as this question can now be solved, but people coming onto this question will first read your (then correct, now incorrect) comment and may be mislead as a result. – Steve Dodier-Lazaro Aug 19 '23 at 09:37

3 Answers3

191

See TS4.1 ANSWER:

type WeekdayType = `${Weekday}`;

PRE TS-4.1 ANSWER:

This can't be done programmatically... you're trying to convert the type Weekday, which is Weekday.MONDAY | Weekday.TUESDAY | Weekday.WEDNESDAY, to the type WeekdayType which is "mon" | "tue" | "wed". This conversion is a form of widening, since Weekday is a subtype of WeekdayType:

type WeekdayExtendsWeekdayType = 
  Weekday extends WeekdayType ? true : false
// type WeekdayExtendsWeekdayType = true

Unfortunately the compiler doesn't give you a handle to remove an "enum"-ness from the enum type and leave you with plain literal types.


So, workarounds? Maybe you don't actually need an enum, but can make do with an object whose property values are string literals:

const lit = <V extends keyof any>(v: V) => v;
const Weekday = {
  MONDAY: lit("mon"),
  TUESDAY: lit("tue"),
  WEDNESDAY: lit("wed")
}
type Weekday = (typeof Weekday)[keyof typeof Weekday],

If you inspect it, the value named Weekday behaves like an enum object:

console.log(Weekday.TUESDAY); // tue

while the type named Weekday behaves like the union of string values "mon" | "tue" | "wed" that you were calling WeekdayType:

const w: Weekday = "wed"; // okay
const x: Weekday = "xed"; // error

So in this workaround, there is no "enum"-ness, and therefore no need to distinguish the type Weekday from the type WeekdayType. It's a little different from an actual enum (which includes types like Weekday.MONDAY, which you'd have to represent as the cumbersome typeof Weekday.MONDAY or create a different type alias for it), but it might behave similarly enough to be useful. Does that work for you?

starball
  • 20,030
  • 7
  • 43
  • 238
jcalz
  • 264,269
  • 27
  • 359
  • 360
69

TypeScript 4.1+:

As mentioned, this can be achieved by using Template Literal Types like so:

type WeekdayType = `${Weekday}`;

TypeScript 3.4+:

Following up on @jcalz answer and the comment from @just-boris, here's an example with const assertions:

const Weekday = {
  MONDAY: "mon",
  TUESDAY: "tue",
  WEDNESDAY: "wed",
} as const;

type Weekday = (typeof Weekday)[keyof typeof Weekday];

Edit:

Wrote a blog post for those who would like to dig deeper.

designcise
  • 4,204
  • 1
  • 17
  • 13
  • 1
    omigod thank you. I was flailing for hours trying to figure this out. – Seth Bro Oct 24 '20 at 00:47
  • That should work but has a limitation. ``` type Weekday1 = (typeof Weekday)[keyof typeof Weekday]; type Weekday2 = `${Weekday}`; const day1: Weekday1 = Weekday.MONDAY; => works const day2: Weekday2 = Weekday.MONDAY; => works const day3: Weekday1 = 'mon' => does not work const day4: Weekday2 = 'mon'; => works ``` – Mina Luke Jan 04 '21 at 22:30
52

With Typescript 4.1, it can be done!

enum Weekday {
  MONDAY = 'mon',
  TUESDAY = 'tue',
  WEDNESDAY = 'wed'
}

type WeekdayType = `${Weekday}`;

And for number enums, thanks to @okku:

enum ThreeDigits {
  ZERO = 0,
  ONE = 1,
  TWO = 2
}

type ThreeDigitsType = `${ThreeDigits}` extends `${infer T extends number}` ? T : never;
coyotte508
  • 9,175
  • 6
  • 44
  • 63