字符串枚举的反向映射

Kam*_*jar 18 javascript msdn node.js typescript visual-studio-code

我想在typescript中使用字符串枚举,但我看不到支持反向映射.我有这样的枚举:

enum Mode {
    Silent = "Silent",
    Normal = "Normal",
    Deleted = "Deleted"
}
Run Code Online (Sandbox Code Playgroud)

我需要像这样使用它:

let modeStr: string;
let mode: Mode = Mode[modeStr];
Run Code Online (Sandbox Code Playgroud)

是的我不知道它在modeStr字符串中是什么,我需要将它解析为枚举,或者如果字符串未在枚举定义中显示则在运行时解析失败.我怎么能尽可能整洁呢?提前致谢

Rod*_*ris 15

我们可以使它Mode成为同一类型的类型和值.

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"
Run Code Online (Sandbox Code Playgroud)

更严格的版本:

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
Run Code Online (Sandbox Code Playgroud)

String Enum作为这个答案:

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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢...它可以在打字稿中实现。 (2认同)
  • tslint 将在该 Enum 示例上抛出错误:*Element 隐式具有 'any' 类型,因为索引表达式的类型不是 'number'*。我想问题是在 TS 字符串枚举中不能反向映射,请参阅 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4 上的字符串枚举示例中的注释.html - 这对于引入 String-Enum 的 TS 2.4 来说似乎是正确的,但我也在 TS 2.6.2 中得到了错误 (2认同)

Phi*_*Lho 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);
});
Run Code Online (Sandbox Code Playgroud)

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.