A fork of enumify that enables Java-like enums in TypeScript.
npm install ts-enumstext
npm install ts-enums
`
Use:
`typescript
import {Enum, EnumValue} from 'ts-enums';
export class Color extends EnumValue {
constructor(name: string) {
super(name);
}
}
class ColorEnumType extends Enum {
RED: Color = new Color('RED name');
GREEN: Color = new Color('GREEN name');
BLUE: Color = new Color('BLUE name');
constructor() {
super();
this.initEnum('Color');
}
}
export const ColorEnum: ColorEnumType = new ColorEnumType();
// examples of use
console.log(ColorEnum.RED.toString()); // Color.RED
console.log(ColorEnum.GREEN instanceof Color); // true
new Color();
// Error: EnumValue classes can’t be instantiated individually
`
Unfortunately, this is not as terse as Enumify's syntax. Here are the steps:
1. We define the implementation of EnumValue that defines each of the instances.
2. We implement each instance of the EnumValue as a property on the Enum. Within the Enum, we call initEnum() with a unique name to set up all of the Enum-specific behavior.
3. We export an instance of the enum so that other modules can use it.
Properties of Enum classes
Enum exposes the getter values, which produces an Array with all enum values:
`typescript
for (const c of ColorEnum.values) {
console.log(c.toString());
}
// Output:
// Color.RED
// Color.GREEN
// Color.BLUE
`
Enum exposes the methods byPropName and byDescription, to extract the EnumValue instance by either the property name of the object in the Enum or the description string passed into the EnumValue's constructor, respectively:
`typescript
console.log(ColorEnum.byPropName('RED') === ColorEnum.RED); // true
console.log(ColorEnum.byDescription('RED name') === ColorEnum.RED); // true
true
`
Properties of enum values
Ts-Enums adds two properties to every enum value:
* propName: the property name of the object in the Enum.
`repl
> ColorEnum.BLUE.name
'BLUE'
`
* ordinal: the position of the enum value within the Array values.
`repl
> ColorEnum.BLUE.ordinal
2
`
Adding properties to enum values
The EnumValues are full TypeScript classes, enabling you to add properties and methods (see the tests for more examples).
`typescript
import {Enum, EnumValue} from 'ts-enums';
import {Color, ColorEnum} from 'color';
class BridgeSuit extends EnumValue {
constructor(name: string, private _isMajor: boolean = false) {
super(name);
}
// can use simple properties (defensively-copied
// to maintain immutability)
get isMajor(): boolean{
return this._isMajor;
}
// can also use methods, though this isn't an ideal implementation.
// (I would probably used another property instead of an if-else)
get color(): Color {
if (this === BridgeSuiteEnum.SPADES
|| this === BridgeSuiteEnum.CLUBS) {
return ColorEnum.BLACK;
} else {
return ColorEnum.RED;
}
}
}
class BridgeSuitEnumType extends Enum {
SPADES: BridgeSuit = new BridgeSuit('Spades', true);
HEARTS: BridgeSuit = new BridgeSuit('Hearts', true);
DIAMONDS: BridgeSuit = new BridgeSuit('Diamonds');
CLUBS: BridgeSuit = new BridgeSuit('Clubs');
constructor() {
super();
this.initEnum('BridgeSuit');
}
// can also have methods on the overall type
get majors(): BridgeSuit[] {
return this.values.filter((suit: BridgeSuit ) => suit.isMajor);
}
}
const BridgeSuitEnum: BridgeSuitEnumType =
new BridgeSuitEnumType();
console.log(BridgeSuitEnum.HEARTS.color.toString()); // Color.RED
console.log(BridgeSuitEnum.HEARTS.isMajor); // true
// ``