我需要在 Typescript 中创建一个枚举。但是,您只能使用基于整数的枚举,例如 C#。 C# 仍然有它的结构来执行非整数相关的事情。这似乎没有在 TypeScript 中实现。我是否需要“变通”或者是否实施了某些措施?
我试图实现的 TypeScript 与此 Java 代码等效:
public enum Something {
PENNY("PENNY"), NICKLE("NICKLE");
private String value;
private Something (String value) {
this.value = value;
}
};
现在是 [电子邮件受保护],但是像 java 或 c# 这样的枚举 - 不存在。 所以我写了一些解决方法。
/**
* Decorator for Enum.
* @param {string} idPropertyName - property name to find enum value by property value. Usage in valueOf method
* @return constructor of enum type
*/
export function Enum<T = any>(idPropertyName?: keyof T) {
// tslint:disable-next-line
return function <T extends (Function & EnumClass)>(target: T): T {
const store: EnumStore = {
name: target.prototype.constructor.name,
enumMap: {},
enumMapByName: {},
enumValues: [],
idPropertyName: idPropertyName
};
// Lookup static fields
for (const fieldName of Object.keys(target)) {
const value: any = (target as any)[fieldName];
// Check static field: to be instance of enum type
if (value instanceof target) {
const enumItem: Enumerable = value;
let id = fieldName;
if (idPropertyName) {
id = (value as any)[idPropertyName];
if (typeof id !== "string" && typeof id !== "number") {
const enumName = store.name;
throw new Error(`The value of the ${idPropertyName} property in the enumeration element ${enumName}.${fieldName} is not a string or a number: ${id}`);
}
}
if (store.enumMap[id]) {
const enumName = store.name;
throw new Error(`An element with the identifier ${id}: ${enumName}.${store.enumMap[id].enumName} already exists in the enumeration ${enumName}`);
}
store.enumMap[id] = enumItem;
store.enumMapByName[fieldName] = enumItem;
store.enumValues.push(enumItem);
enumItem.__enumName__ = fieldName;
Object.freeze(enumItem);
}
}
target.__store__ = store;
Object.freeze(target.__store__);
Object.freeze(target);
return target;
};
}
/** Key->Value type */
export type EnumMap = {[key: string]: Enumerable};
/** Type for Meta-Data of Enum */
export type EnumClass = {
__store__: EnumStore
};
/** Store Type. Keep meta data for enum */
export type EnumStore = {
name: string,
enumMap: EnumMap,
enumMapByName: EnumMap,
enumValues: Enumerable[],
idPropertyName?: any
};
/** Enum Item Type */
export type EnumItemType = {
__enumName__: string;
};
/** Interface for IDE: autocomplete syntax and keywords */
export interface IStaticEnum<T> extends EnumClass {
new(): {enumName: string};
values(): ReadonlyArray<T>;
valueOf(id: string | number): T;
valueByName(name: string): T;
}
/** Base class for enum type */
export class Enumerable implements EnumItemType {
// tslint:disable:variable-name
// stub. need for type safety
static readonly __store__ = {} as EnumStore;
// Initialize inside @Enum decorator
__enumName__ = "";
// tslint:enable:variable-name
constructor() {
}
/**
* Get all elements of enum
* @return {ReadonlyArray<T>} all elements of enum
*/
static values(): ReadonlyArray<any> {
return this.__store__.enumValues;
}
/**
* Lookup enum item by id
* @param {string | number} id - value for lookup
* @return enum item by id
*/
static valueOf(id: string | number): any {
const value = this.__store__.enumMap[id];
if (!value) {
throw new Error(`The element with ${id} identifier does not exist in the $ {clazz.name} enumeration`);
}
return value;
}
/**
* Lookup enum item by enum name
* @param {string} name - enum name
* @return item by enum name
*/
static valueByName(name: string): any {
const value = this.__store__.enumMapByName[name];
if (!value) {
throw new Error(`The element with ${name} name does not exist in the ${this.__store__.name} enumeration`);
}
return value;
}
/** Get enum name */
get enumName(): string {
return this.__enumName__;
}
/** Get enum id value or enum name */
toString(): string {
const clazz = this.topClass;
if (clazz.__store__.idPropertyName) {
const self = this as any;
return self[clazz.__store__.idPropertyName];
}
return this.enumName;
}
private get topClass(): EnumClass {
return this.constructor as any;
}
}
/** 'Casting' method to make correct Enum Type */
export function EnumType<T>(): IStaticEnum<T> {
return (<IStaticEnum<T>> Enumerable);
}
现在,很容易使用了
// node-module
// import {Enum, EnumType} from "ts-jenum";
@Enum("value")
class Something extends EnumType<Something>() {
static readonly PENNY = new Something("Penny");
static readonly NICKLE = new Something("Nickle");
constructor(readonly value: string) {
super();
}
}
// Usage example
console.log("" + Something.PENNY); // Penny
console.log("" + Something.NICKLE); // Nickle
console.log(Something.values()); // [Something.PENNY, Something.NICKLE]
console.log(Something.valueByName("PENNY")); // Something.PENNY
console.log(Something.PENNY.enumName); // PENNY
以上所有内容都是类型安全的。
虽然您无法给出明确的字符串值(只能是数字),但 TypeScript 枚举可以通过它们的索引和名称来引用。您可以通过将数字传递回枚举来访问他们的名字,如下所示:
enum Direction { Up, Down, Left, Right }
console.info(Direction[Direction.Up]); // "Up"
或者,使用地图:
const directionText = new Map([
[Direction.Up, 'UP'],
// ...
]);
在查看这个问题时,我发现自 typescript 2.4 以来,他们已经有了字符串枚举支持。类似于java的enum,使用起来非常直观。
字符串枚举
TypeScript 2.4 现在允许枚举成员包含字符串初始值设定项。
enum Colors {
Red = "RED",
Green = "GREEN",
Blue = "BLUE", }
需要注意的是,字符串初始化的枚举无法反向映射以获取原始枚举成员名称。换句话说,你不能编写 Colors["RED"] 来获取字符串“Red”。
您可以像这样简单地创建一个枚举。它的工作方式与 Java 或 C# 中相同。
export enum ControlType {
INPUT,
SELECT,
DATEPICKER
}
console.log(ControlType.INPUT); // returns 0
console.log(ControlType[ControlType.INPUT]); // returns INPUT
您还可以添加其他信息,但正如您所注意到的,只能使用类型编号。
export enum ControlType {
INPUT = 3,
SELECT = 6,
DATEPICKER = "abc".length // computed member
}
console.log(ControlType.INPUT); // returns 3
当您想要添加其他非数字类型的成员时,事情会变得更加困难,但有一个解决方法。您必须将类型设置为
<any>
export enum ControlType {
INPUT = <any>'input',
SELECT = <any>'select',
DATEPICKER = <any>'date-picker'
}
console.log(ControlType.INPUT); // returns 'input'
这里的缺点是您只能将这些值与
any
类型的其他变量进行匹配。例如在以下情况下;
(ControlType.INPUT === myVar) {..}
myVar 必须声明为
myVar: any = 'input';
,即使它是一个字符串。
很多人没有意识到 Java 枚举到底是什么(一组共享基类的静态单例)
这与基于原始的枚举“确实”不同。 在 C# 中,您可以在枚举上添加扩展方法,但不存在多态性。 在 Typescript 中,你只需要使用静态函数即可。 密封类中也有一个类似的新范例,它不是静态的,可以只使用接口而不是抽象基类,并且可以直接参与基于构造函数的依赖注入(枚举实例只能有令人讨厌的属性注入)。
C# 也有密封类,但 Typescript 甚至没有 Final,更不用说密封了,因此您需要使用并行接口和带有开关的工厂进行模拟。