【问题标题】:How to create Class-based Enums in Javascript/Typescript如何在 Javascript/Typescript 中创建基于类的枚举
【发布时间】:2021-01-26 17:19:34
【问题描述】:

我有这个 python 代码,我正在尝试将其转换为 Javascript/Typescript

import enum

class Shape(enum.Enum):
    RECTANGLE = 0
    CIRCLE = 1
    TRIANGLE = 2
    OTHER = 3

print(isinstance(data, Shape))

在 Typescript 中,我可以使用枚举,但这不是我想要的。我需要这样做的能力:

const data = new Shape().RECTANGLE;
console.log(data instanceof Shape); // should return true

使用枚举或对象是不可能做到的。

编辑:在 Typescript 中构建这种基于类的枚举的语法是什么?

【问题讨论】:

  • (data in Shape) 适合你吗?
  • 你能解释更多吗? new Shape() 显然是 instanceof Shape,但为什么 someShape.RECTANGLE 也会是 instanceof Shape?这是如何使用的?
  • @CharlesBamford 你能举个例子吗?
  • @AlexWayne 我正在尝试获取在打字稿中创建这种“基于类的枚举”的语法
  • 类似const Shapes = {RECTANGLE: "RECTANGLE", OTHER: "OTHER"}; const data = Shapes.OTHER; console.log(data in Shapes);

标签: javascript python typescript class enums


【解决方案1】:

TypeScript 中没有用于定义基于类的枚举的特定“语法”。但这里有一个名为 Shape 的类的示例,它可以识别四种类型的形状。

Shape 提供了一个接受参数shapeType 的构造函数。如果shapeType 不是可识别的类型,create 会抛出异常。

class Shape {
  constructor(public shapeType: number) {
    if (!Shape.types.includes(shapeType)) {
      throw new Error(`Value ${shapeType} is not a valid shape type.`);
    }    
  }

  static readonly rectangle: number = 0;
  static readonly circle: number = 1;
  static readonly triangle: number = 2;
  static readonly other: number = 3;

  static get types(): number[] {
    return [
      Shape.rectangle,
      Shape.circle,
      Shape.triangle,
      Shape.other,  
    ];
  }
}

用法:

const shape1: Shape = new Shape(Shape.triangle);
const isInstanceOf: boolean = shape1 instanceof Shape;
console.log(`Is Shape? ${isInstanceOf}; type is ${shape1.shapeType}`); // Is Shape? true; type is 2

try {
  const shape2: Shape = new Shape(42);
} catch (e) {
  console.log(e.message); // Value 42 is not a valid shape type.
}

有关您可能希望从枚举转移到类的原因,请参阅C# vs Java Enum (for those new to C#)

【讨论】:

    【解决方案2】:

    枚举是一个打字稿概念,并记录在here。当你编写一个枚举时,typescript 正在创建一个与枚举的选项匹配的纯 js 对象,因此可以使用“in”运算符来检查提供的值是否是枚举的成员。

    打字稿枚举。

    enum Direction {
      Up,
      Down,
      Left,
      Right,
    }
    

    javascript 中的内容。

    var Direction = {
      '0': 'Up',
      '1': 'Down',
      '2': 'Left',
      '3': 'Right',
      Up: 0,
      Down: 1,
      Left: 2,
      Right: 3
    }
    

    检查一个值是否是枚举的成员可以通过

    const foo = "Up";
    
    // Unsafe, but quick and easy.
    console.log(foo in Direction); // returns true.
    console.log("toString" in Direction); // also returns true.
    
    // Safe.
    console.log(Direction.hasOwnProperty(foo)); // returns true.
    console.log(Direction.hasOwnProperty(foo)); // returns false.
    

    【讨论】:

    • 谢谢查尔斯。虽然这确实有帮助,但这并不是我想要的。我宁愿看一个使用类而不是对象的示例。
    • 这不是打字稿做枚举的方式。您可以推出自己的实现,但您将独自完成。
    • 我不一定需要使用enum,只需一个基本的class 代替
    【解决方案3】:

    这是在 JS/TS 中创建枚举的方法 -

    const enum OneDBServerPermission {
      RECTANGLE = 0
      CIRCLE = 1
      TRIANGLE = 2
      OTHER = 3
    }
    

    如果你想在其他类中使用它,你可以使用export关键字

    export const enum OneDBServerPermission {
      RECTANGLE = 0
      CIRCLE = 1
      TRIANGLE = 2
      OTHER = 3
    }
    

    【讨论】:

    • 我知道如何在 JS/TS 中创建枚举,谢谢。不过,我正在寻找基于类的枚举。
    猜你喜欢
    • 2012-09-23
    • 2013-10-05
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多