7

I'd like to write something like this in Typescript:

export function stringToEnum<T>(enumObj: T, str: string): keyof T {
    return enumObj[str];
}

and use it as follows:

enum MyEnum {
  Foo
}

stringToEnum<MyEnum>(MyEnum, 'Foo');

where it would return

MyEnum.Foo

The function above works as expected... but the typings are throwing errors. For the parameter MyEnum in stringToEnum<MyEnum>(MyEnum, 'Foo');, Typescript complains tha:

Argument of type 'typeof MyEnum' is not assignable to parameter of type 'MyEnum'

which makes sense... unfortunately. Any ideas on how I can get around this?

sir_thursday
  • 5,270
  • 12
  • 64
  • 118

3 Answers3

10

You can do it all natively without having to write a function:

enum Color {
    red,
    green,
    blue
}

// Enum to string
const redString: string = Color[Color.red];
alert(redString);

// String to enum
const str = 'red';
const redEnum: Color = Color[str];
alert(redEnum);

Or you can have some fun with it...

enum MyEnum {
  Foo,
  Bar
}

function stringToEnum<ET, T>(enumObj: ET, str: keyof ET): T{
    return enumObj[<string>str];
}

const val = stringToEnum<typeof MyEnum, MyEnum>(MyEnum, 'Foo');

// Detects that `foo` is a typo
const val2 = stringToEnum<typeof MyEnum, MyEnum>(MyEnum, 'foo');
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Right. Is there any way to do it with a method, though? – sir_thursday Jan 25 '18 at 19:26
  • 1
    I've added an example for fun... the call to the function is significantly trickier than just using `MyEnum['Foo']`. – Fenton Jan 25 '18 at 19:36
  • 1
    Hey @Fenton. TS compiler errors out with 7053 in current TS versions, also within your function. [This](https://blog.oio.de/2014/02/28/typescript-accessing-enum-values-via-a-string/) explains it. So the easiest solution would be: `const str: string = 'red'; const redEnum: Color = (Color)[str];` – JackLeEmmerdeur Jan 23 '20 at 18:08
  • 1
    In addition to my last comment: If you got an enum like `enum Color { Red = "red"}` then `const redEnum: Color = (Color)["red"]` won't match as the indexer matches the enum-member-name. So only `const redEnum: Color = (Color)["Red"]` would work. – JackLeEmmerdeur Jan 23 '20 at 18:22
3

Your signature is a bit mixed up. The return type should be T[keyof T] if you intend for the method to return an enum value. The type of the str param should also be keyof T to prevent you from passing invalid strings in, but this will limit you to passing string literals in (or well-typed variables of type keyof T, but not string):

function stringToEnum<T>(enumObj: T, str: keyof T): T[keyof T]

Then either don't specify the type param and let the compiler infer the type correctly:

// type: Foo
// value: 0  
const result = stringToEnum(MyEnum, 'Foo');

Or you need to provide typeof MyEnum as the type param:

// type: Foo
// value: 0  
const result = stringToEnum<typeof MyEnum>(MyEnum, 'Foo');

If you really want to be able to pass in any arbitrary string enum name, then the return type is a lie: should be T[keyof T] | undefined. You'll also run into trouble when attempting enumObj[str] if the type of str is string and you have noImplicitAny compiler option enabled.

There's a bit more to making generic functions that work with enum types properly, especially numeric enums that have reverse lookup entries at run-time. Take a look at the source code for ts-enum-util (github, npm) for inspiration

Jeff Lau
  • 433
  • 4
  • 4
1
 stringToEnum(MyEnum, 'Foo');

Just leave away the generic and let typescript do that. Thats because the type stored under MyEnum does not match the Enum itself but is a union type of its values:

 enum Test { A, B };

 const value: Test /* "A" | "B" */ = Test.A;
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151