Reverse-Mapping for String Enums

后端 未结 4 668
星月不相逢
星月不相逢 2020-12-05 23:12

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         


        
相关标签:
4条回答
  • 2020-12-05 23:50

    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[<any>mode];
        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.

    0 讨论(0)
  • 2020-12-05 23:55

    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];
    
    0 讨论(0)
  • 2020-12-06 00:03

    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
    
    0 讨论(0)
  • 2020-12-06 00:10

    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>();
    
    0 讨论(0)
提交回复
热议问题