【问题标题】:How can I easily convert from numeric Enums to string or number and vice versa in Typescript?如何在 Typescript 中轻松地将数字枚举转换为字符串或数字,反之亦然?
【发布时间】:2020-05-06 20:52:31
【问题描述】:

这很令人困惑,所有不同的语法。我希望有一种简单、整洁的方法来处理我的代码中的任何类似情况,而不必总是想知道如何编写它。

enum Direction {
  Up, Down, Left, Right
}

我想通过以下方式在我的代码中转换Direction 或任何其他Enum

  • 枚举 字符串
  • 枚举 数字
  • 数字字符串

例子:

let test = Direction.Left;
enumToString(test); // -> 'Left'
enumToNumber(test); // -> 2
stringToNumber('Left'); // -> 2
stringToEnum('Left'); // -> Direction.Left

【问题讨论】:

    标签: typescript enums type-conversion


    【解决方案1】:

    经过一些修改,我能够在满足 Typescript 编译器的同时制作所有请求的转换器函数(使用 Generics)。我还添加了stringExistsnumberExists 来检查密钥是否存在。

    两个重要警告:它只适用于numeric Enums,我只用 Typescript 3.7.5

    测试过它
    // For *numeric* Enums !
    export class EnumUtils {
      public static numberToString<T>(enumType: T, n: number): string {
        return enumType[n as keyof T] + '';
      }
      public static stringToEnum<T>(enumType: T, s: string): T[keyof T] {
        return enumType[s as keyof T];
      }
      public static enumToNumber<T>(enumType: T, s: T[keyof T]): number {
        return +s; // enumType parameter must not be removed (for type checking)
      }
      public static stringToNumber<T>(enumType: T, s: string): number {
        return EnumUtils.enumToNumber(enumType, EnumUtils.stringToEnum(enumType, s));
      }
      public static numberToEnum<T>(enumType: T, n: number): T[keyof T] {
        return EnumUtils.stringToEnum(enumType, EnumUtils.numberToString(enumType, n));
      }
      public static enumToString<T>(enumType: T, s: T[keyof T]): string {
        return EnumUtils.numberToString(enumType, EnumUtils.enumToNumber(enumType, s));
      }
      public static stringExists<T>(enumType: T, s: string): boolean {
        return s === EnumUtils.enumToString(enumType, EnumUtils.stringToEnum(enumType, s));
      }
      public static numberExists<T>(enumType: T, n: number): boolean {
        return n === EnumUtils.enumToNumber(enumType, EnumUtils.numberToEnum(enumType, n));
      }
    }
    

    将其保存在 'enum-utils.ts' 文件中后,您可以执行以下测试:

    import { EnumUtils } from './enum-utils.ts';
    
    enum Direction {
      Up, Down, Left, Right
    }
    
    const test1: Direction = EnumUtils.stringToEnum(Direction, 'Left');
    const test2: string = EnumUtils.enumToString(Direction, Direction.Left);
    const test3: Direction = EnumUtils.numberToEnum(Direction, 2);
    const test4: number = EnumUtils.enumToNumber(Direction, Direction.Left);
    const test5: number = EnumUtils.stringToNumber(Direction, 'Left');
    const test6: string = EnumUtils.numberToString(Direction, 2);
    const test7: boolean = EnumUtils.stringExists(Direction, 'Left');
    const test8: boolean = EnumUtils.stringExists(Direction, 'randomstring');
    const test9: boolean = EnumUtils.stringExists(Direction, '2');
    const test10: boolean = EnumUtils.numberExists(Direction, 2);
    const test11: boolean = EnumUtils.numberExists(Direction, 999);
    
    // true true true true true true true true true true true
    console.log(
      test1 === Direction.Left,
      test2 === 'Left',
      test3 === Direction.Left,
      test4 === 2,
      test5 === 2,
      test6 === 'Left',
      test7 === true,
      test8 === false,
      test9 === false,
      test10 === true,
      test11 === false
    );
    

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 2015-11-18
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多