45

I wanted to use string enums in typescript but I can't see a support for reversed mapping in it. I have an enum like this:

enum Mode {
    Silent = "Silent",
    Normal = "Normal",
    Deleted = "Deleted"
}

and I need to use it like this:

let modeStr: string;
let mode: Mode = Mode[modeStr];

and yes I don't know what is it there in modeStr string and I need it parsed to the enum or a fail at parsing in runtime if the string is not presented in the enum definition. How can I do that as neat as it can be? thanks in advance

3
  • 1
    Possible duplicate of Create an enum with string values in Typescript Jul 3, 2017 at 10:41
  • 8
    @ponury-kostek Technically, that question does not address the issue of making a reverse mapping. Even if it does, it probably sits in one of the many answers hidden at the bottom, making the solution too hard to find. I say if there isn't a better duplicate, let's keep this one.
    – E_net4
    Jul 3, 2017 at 17:05
  • It's not a duplicate! Aug 17, 2022 at 15:26

7 Answers 7

31

We can make the Mode to be a type and a value at the same type.

type Mode = string;
let Mode = {
    Silent: "Silent",
    Normal: "Normal",
    Deleted: "Deleted"
}

let modeStr: string = "Silent";
let mode: Mode;

mode = Mode[modeStr]; // Silent
mode = Mode.Normal; // Normal
mode = "Deleted"; // Deleted
mode = Mode["unknown"]; // undefined
mode = "invalid"; // "invalid"

A more strict version:

type Mode = "Silent" | "Normal" | "Deleted";
const Mode = {
    get Silent(): Mode { return "Silent"; },
    get Normal(): Mode { return "Normal"; },
    get Deleted(): Mode { return "Deleted"; }
}

let modeStr: string = "Silent";
let mode: Mode;

mode = Mode[modeStr]; // Silent
mode = Mode.Normal; // Normal
mode = "Deleted"; // Deleted
mode = Mode["unknown"]; // undefined
//mode = "invalid"; // Error

String Enum as this answer:

enum Mode {
    Silent = <any>"Silent",
    Normal = <any>"Normal",
    Deleted = <any>"Deleted"
}

let modeStr: string = "Silent";
let mode: Mode;

mode = Mode[modeStr]; // Silent
mode = Mode.Normal; // Normal
//mode = "Deleted"; // Error
mode = Mode["unknown"]; // undefined
8
  • 2
    Thanks...It could've been implemented in the typescript though. Jul 12, 2017 at 8:27
  • 3
    tslint will throw an error on that Enum example: Element implicitly has an 'any' type because index expression is not of type 'number'. I guess the issue is that in TS string Enums can not be reverse-mapped, see the comment in the String-Enum example at typescriptlang.org/docs/handbook/release-notes/… - This seems to be true for TS 2.4 where String-Enum was introduced but I also get the error in TS 2.6.2
    – omni
    Dec 29, 2017 at 8:45
  • 1
    @masi Since 2.4, TS has support for string enums, so you don't need to add the <any> cast.
    – Rodris
    Dec 29, 2017 at 13:09
  • 1
    @RodrigoPedrosa I'm aware of that. The error occues with and without the <any> cast. There's a GIT issue open related to that problem (github.com/Microsoft/TypeScript/issues/9234) but it was closed as "working as intended". Seems like TS devs never used any other lang that supports String-Evals otherwise they wouldn't claim it works as intended. At best it works "as good as possible" for a lang that tries to bring types to an other lang (I have not checked the tech. reasons so far thought).
    – omni
    Dec 29, 2017 at 13:10
  • @masi I am not sure about what you are saying. The reverse mapping works in the playground. Is your problem specifically with the tslint?
    – Rodris
    Dec 29, 2017 at 13:23
8

The cleanest way I found so far is to make a secondary map:

let reverseMode = new Map<string, Mode>();
Object.keys(Mode).forEach((mode: Mode) => {
    const modeValue: string = Mode[mode as any];
    reverseMode.set(modeValue, mode);
});

Thus you can do let mode: Mode = reverseMode.get('Silent');

Advantages: no need to repeat the values, provides a way to enumerate the enum, keeps TSLint happy...

Edit: I originally write Mode[mode] but then TS might throw the error TS7015 on this line, so I added the cast.

2
  • typescript <> casts don't work w/ jsx, use Mode[mode as any] instead of Mode[<any>mode] Apr 20 at 15:46
  • 1
    Beside, it is cleaner your way, <any>mode is deprecated in favor of mode as any. This is an old post… :-)
    – PhiLho
    May 22 at 13:14
4

If you're ok with using Proxies, I made a more generic & compact version:

stringEnum.ts

export type StringEnum<T extends string> = {[K in T]: K}
const proxy = new Proxy({}, {
  get(target, property) {
    return property;
  }
})
export default function stringEnum<T extends string>(): StringEnum<T> {
  return proxy as StringEnum<T>;
}

Usage:

import stringEnum from './stringEnum';
type Mode = "Silent" | "Normal" | "Deleted";
const Mode = stringEnum<Mode>();
2

This answer is from @PhiLho,

I dont have enough rep to comment on his message.

let reverseMode = new Map<string, Mode>();
Object.keys(Mode).forEach((mode: Mode) => {
    const modeValue: string = Mode[<any>mode];
    reverseMode.set(modeValue, mode);
});

However, the first argument for .set should be the key, and the second should be the value.

reverseMode.set(modeValue, mode);

should be...

reverseMode.set(mode, modeValue);

Resulting in this...

let reverseMode = new Map<string, ErrorMessage>();
Object.keys(ErrorMessage).forEach((mode: ErrorMessage) => {
    const modeValue: string = ErrorMessage[<any>mode];
    reverseMode.set(mode, modeValue);
});

The reason why @PhiLho might have missed this is because the keys and values for the original object provided by OP are the same.

1

None of the answers really worked for me, so I have to fall back to normal for the loop. my enum is

enum SOME_CONST{
      STRING1='value1', 
      STRING2 ='value2',
      .....and so on
}


getKeyFromValue =(value:string)=> Object.entries(SOME_CONST).filter((item)=>item[1]===value)[0][0];
1

For all of you still struggling with this, this is a fast and dirty way of getting this done.

enum Mode {
    Silent = "Silent",
    Normal = "Normal",
    Deleted = "Deleted"
}

const currMode = 'Silent',
  modeKey = Object.keys(Mode)[(Object.values(Mode) as string[]).indexOf(currMode)];

However, there are some caveats, that you should be aware of!

The most likely reason Microsoft marked the Git bug as "working as intended", is probably due to the fact that this deals with String Literals where as enums were originally designed to be indexable. While the above code works and lints with TSLint, it has two issues. Objects are not guaranteed to retain the intended order when converted to an Array and we are dealing with String Literals which may or may not be unique.

Take a look at this StackBlitz where there are two Mode's with values of 'Silent'. You have zero guarantee that it won't return 'Ignore' over 'Silent' once the lookup completes.

https://stackblitz.com/edit/typescript-hyndj1?file=index.ts

1

The standard types achieves this function - just check for undefined and handle however.

type Mode = "Silent" | "Normal" | "Deleted";
const a = Mode["something else"];
//a will be undefined

Also, you can add on a record type to the above to extend the "enum", say to translate a short enum key into more verbose output (https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type):

const VerboseMode: Record<Mode, string> = {
  Silent: "Silent mode",
  Normal: "Normal mode",
  Deleted: "This thing has been deleted"
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.