TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

Enumerations package used in Decentra project

Gives unified enum experience for typescript and JS contexts.

NOTE : API changed at version 2 to fix issues with enum members resolution and intellisense support.

Now enum values are separated from resolution function.



import {Enum, makeEnumeration, INCREMENT} from "@decentra/enums";
class PizzaType extends Enum {}
let [PizzaTypes, PizzaTypesResolver] = makeEnumeration(PizzaType, {
    SEAFOOD: "seafood",
    BEACON: "beacon",
    MARGARITA: "margarita"

assert(PizzaTypes.SEAFOOD === PizzaTypesResolver('seafood'));

class DataType extends Enum {};

let [DataTypes, DataTypesResolver] = makeEnumeration(DataType, {
    NULL : new DataType('null', 1),
    UNDEFINED : new DataType('undefined', 2),
    STRING : new DataType('string', 3),
    NUMBER : new DataType('number', 4),
    BOOLEAN: new DataType('boolean', 5)

assert(DataTypes.BOOLEAN === DataTypesResolver('boolean'));
assert(DataTypes.BOOLEAN !== PizzaTypes.BEACON);
assert(DataTypes.BOOLEAN !== PizzaTypesResolver('margarita'));

class SmallPrimeNumber extends Enum {}
let [PrimeNumbers, PrimesResolver, AllPrimes] = makeEnumeration(SmallPrimeNumber, {
    'one': 1,
    'two': 2,
    'three': 3,
    'five': 5,
    'seven': 7,
    'eleven': 11
strictEqual(AllPrimes.length, Object.entries(PrimeNumbers).length);
for(let prime of AllPrimes) {
    strictEqual(prime.isNumber, true);


Project written in pure JS (no typescript) for easier testing and local startup, and reduce dependencies tree. But pure JS lacks any enum support at language level.

Side benefit - the enum members can be any object extending enum type, by some properties or even methods, which can be useful to bind some processing selected by a value of an "enum". Key gotcha to watch for is to begin constructor signature with name and value pair, and use creation approach as it defined for DataTypes example. In this case you can add some flags and other properties to enumeration members configured via constructor. It can be used for logging severity levels and other scenarios

Usage in Typescript

This package provides TS declaration file to make sure it can be used in projects with TS as well.



Package Sidebar


npm i @decentra/enums

Weekly Downloads






Unpacked Size

21.4 kB

Total Files


Last publish


  • kote.isaev