A typed enum for TypeScript.
Ever wanted to check if a value was a variant of an enum:
enum MyEnum {
First = 'first',
Second = 'seconds',
}
const value = MyEnum.First;
if (value instanceof MyEnum) { // error
console.info('magic');
}
Now you can:
import { Enum } from '@biro456/enum';
type MyEnum = typeof MyEnum;
const MyEnum = Enum.create([
'First',
'Second',
]);
const value = MyEnum.First;
if (MyEnum.is(value)) { // works
console.info('magic');
}
Mainly because I wanted to see if I could make something ergonomic to use in my projects. So I took several tips from other similar projects.