Skip to content

tylersayshi/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum replacement

Let's deprecate enums!

The typescript team has added --erasableSyntaxOnly to their compiler, which disables the use of enums. This is for compatibility with running .ts files directly from a runtime without a build step. You can read more about it here from Matt Pocock.

If you miss enums

I've written this library to give the feel of enums, but in a way that doesn't require a build step.

Usage

import { numEnum, stringEnum } from "@tyler/enum";

const colors = numEnum(["red", "green", "blue"]);
const animals = stringEnum(["cat", "dog", "bird"]);

console.log(colors.red); // 0
console.log(colors.green); // 1
console.log(colors.blue); // 2

console.log(animals.cat); // cat
console.log(animals.dog); // dog
console.log(animals.bird); // bird

Try it yourself

Playground Link

If you're looking for type errors on comparisons between differently named enums:

Link to example with branded types

Releases

No releases published

Packages

No packages published