一种方式是直接按静态类处理, 扩展性更好些. 任意自增方法或属性
```ts
class TsEnum {
public readonly code: string
public readonly name: string
constructor(code: string, name: string) {
this.code = code
this.name = name
}
public static getInstanceByCode<T extends TsEnum>(this: new (code: string, name: string) => T, code string): T | null {
const enumValues = Object.values(this)
for (const enumValue of enumValues) {
if (enumValue instanceof this && enumValue.code === code) {
return enumValue
}
}
return null
}
}
class SystemEnum extends TsEnum {
public static readonly FOO1 = new SystemEnum('foo1', 'FOO1')
public static readonly FOO2 = new SystemEnum('foo2', 'FOO2')
}
console.log(SystemEnum.getInstanceByCode('foo1'))
console.log(
SystemEnum.FOO1.name)
```