extended-enum
Grant handy utilities and object-oriented powers to TypeScript enums
Extend your TypeScript enum
with magic utility, extend
✨.
import { extend } from 'extended-enum';
enum _Priority { Critical = 100, High = 50, Medium = 30, Low = 10 }
// now, your native enum is extended!
class Priority extends extend<typeof _Priority, _Priority>(_Priority) {}
Out-of-the-box utilities
Parsing, iterating and pattern matching utilties are available from the extended enum instance.
All of the utilities work seamlessly with TypeScript, so don't worry!
// parsing primitives to enums
const priority = Priority.from(50);
// iteration
for (const priority of Priority) { ... }
const keys = [...Priority.keys()];
// matching
const isLow = priority.is('Low');
const isMedium = priority.is(30);
const isHigh = priority.is(Priority.High);
interface IPriority { shouldIgnore(): boolean }
class Priority extends <typeof _Priority, _Priority, IPriority>(_Priority) {
// define methods in addition to extended enum class
shouldIgnore() {
return this.is(Priority.Medium) || this.is(Priority.Low);
}
}
Priority.High.shouldIgnore(); // false
Priority.Low.shouldIgnore(); // true
Attach your business logic to enums
Native TypeScript enum
s are not extensible. Your utilities and business logic should have been defined separately with their definitions.
Extended enums allow your logic to be attached to its definition, in similar way with Java Enums.