56
export enum MyEnum{
    Option1,
    Option2,
    Option3
}


string x = 'Option1';

MyEnum[x] throws an error:

Type string is not assignable to type MyEnum

however: MyEnum['Option1'] works.

I need to use MyEnum[x] though (in a method that returns a MyEnum), where x is a calculated value that results in one of the valid enum options, how do I go about it?

annepic
  • 1,147
  • 3
  • 14
  • 24
  • 1
    Related question and answer: https://stackoverflow.com/questions/36316326/typescript-ts7015-error-when-accessing-an-enum-using-a-string-type-parameter – Neoheurist Aug 26 '21 at 12:02

4 Answers4

132

There are two ways to achieve this

⭐️ 1st: If u still want to keep the powerful typechecking feature of TS, choose this

MyEnum[x as keyof typeof MyEnum] 

typeof MyEnum will create an interface that represents the MyEnum object behind the scene and keyof will return a union of string literals, each one is the key in the MyEnum object (in other words, keyof will return a list of keys of a given object/class).

⭐️ 2nd way: just simply turn off the type checking for the next line, which is similar to asserting the type of MyEnum to <any> as done in the @annepic 's answer

// @ts-ignore 
MyEnum[x] 

Extra note: Always turn on the Strict flag in tsconfig no matter what (u should only ignore type checking for a single line in very rare and special cases as done above). This configuration will force the developers to define the shape/type/structure/interface for everything, which makes the entire codebase super self-documenting and self-explanatory, especially for your future self and code maintainers. U can quickly infer how a class/object is structured and how a function is used with 100% certainty without even looking at its implementation. JSDoc specifies a set of very strict formatting rules for documenting JS code using comments, but these comments often end up being out of date because they are not changed along with the function evolution. Other benefits of the Strict flag are mentioned in the TypeScript primary doc.

Son Nguyen
  • 2,991
  • 2
  • 19
  • 21
  • 11
    Love the `keyof typeof` combo – vancy-pants Feb 24 '21 at 17:21
  • 6
    Appreciate you actually explaining what `keyof typeof` is doing! :) – chucknelson Mar 20 '22 at 00:15
  • Clearly this post does not provide two elegant solutions as it suggests. I almost stop reading at @ts-ignore I really think this should be a last resort, there is always better solutions than that. Thanks for your 2nd one btw! – Salketer Aug 09 '23 at 12:15
4

You are declaring your string x variable wrong. You should do this:

export enum MyEnum{
    Option1,
    Option2,
    Option3
}


var x = 'Option1';
MyEnum[x];
Poku
  • 3,138
  • 10
  • 46
  • 64
  • thanks but it doesn't work like this. the x is calculated outside the method so maybe that's why. interesting to notice that MyEnum[x] is syntax-error highlighted if I previously give x a value of 'Opt'+'ion1'. The concatenation gives a valid value, but typescript doesn't care to analyse it and directly shows a syntax error. – annepic May 18 '18 at 18:43
3

Got it to work like this: return (<any>MyEnum)[x];

annepic
  • 1,147
  • 3
  • 14
  • 24
1

I think what you're looking for is to explicitly type the selected item in your enum as that enum. Example:

export enum MyEnum{
  Option1,
  Option2,
  Option3
}

function getEnum(x = 'Option1'):MyEnum {
  return <MyEnum>MyEnum[x];
}

The error you referenced "Type string is not assignable to type MyEnum" is actually coming from the function return type not matching your enum. A few of the other solutions convert the enum to an object but that isn't compatible with a function that has a return type of the same enum.

Keegan Brown
  • 515
  • 3
  • 5