Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
ts-enum-util
Advanced tools
Strictly typed utilities for working with TypeScript enums (and string/number literal union types).
NOTE: Be sure to read about supported TypeScript versions in the Requirements section.
$enum
?ts-enum-util
provides type-safe utilities to improve the usefulness of TypeScript enums.
There are two major and distinct aspects to ts-enum-util
.
A wrapper around an enum, or "enum-like object", that provides a variety of type-safe utilities in terms of the run-time representation fo the enum's keys/values. Some examples include:
All of these utilities are very specifically typed for each enum via generics and type inference.
A visitor pattern for processing a single value whose type is an enum, or union of
string/number literals. It's like a switch statement that forces you to implement
every possible case (including null
or undefined
, if relevant), avoiding bugs
because you forgot to handle one of the enum's values, or because the enum
definition was updated with a new value and you forgot to update existing code to
handle the new value.
The more generalized "visit" functionallity has you associate a different function with each possible value of an enum or string/number literal union. The appropriate function is executed (and its return value returned) based on which value the argument is at run-time.
A streamlined "map" functionality has you simply associate values (of any type) with each possible value of an enum or string/number literal union. The appropriate mapped value is returned based on which value the argument is at run-time.
Install via NPM:
npm i -s ts-enum-util
Import $enum
:
import { $enum } from "ts-enum-util";
Define an enum
:
enum Color {
R,
G,
B
}
Use $enum()
as a function to access Enum Wrapper Utilities for your enum
:
// type of "values": Color[]
// value of "values": [0, 1, 2]
const values = $enum(Color).getValues();
Use $enum.visitValue()
or $enum.mapValue()
to access Enum Value Visitor/Mapper functionality:
function doColorAction(color: Color): void {
$enum.visitValue(color).with({
[Color.R]: () => {
window.alert("Red Alert!");
},
[Color.G]: () => {
window.location = "http://google.com";
},
[Color.B]: () => {
console.log("Blue");
}
});
}
function getColorLabel(color: Color | undefined): string {
return $enum.mapValue(color).with({
[Color.R]: "Red",
[Color.G]: "Green",
[Color.B]: "Blue",
[$enum.handleUndefined]: "Unspecified"
});
}
To keep the size of the README under control, usage documentation and examples have been split out to separate files:
ts-enum-util
is all about strictly type-safe utilities
around TypeScript enums, so it would be much less useful in a plain JavaScript
project. More specifically, TypeScript 2.9 included advancements in handling
number literals as property names of object types, which is necessary for
implementing some ts-enum-util
functionality consistently for both string and
number enum types.
ts-enum-util
, so they
must exist (either natively or via polyfill) in the run-time environment:
Map
WeakMap
Symbol
Symbol.iterator
Symbol.toStringTag
$enum
?I wanted something short, simple, and easy to remember that was unlikely to conflict with anything else so that no one would have to alias it when importing it. By exporting a clear, memorable, and uniquely named "thing", this allows you to simply start writing code that uses $enum
and most IDEs can take care of inserting the import { $enum } from "ts-enum-util"; for you (either automatically, or with a quick keyboard shortcut).
I ended up using inspiration from the naming of jquery's $()
function. Many javascript developers are familiar with jquery, and the fact that $()
gives you a wrapper around a raw DOM element to expose additional/simplified functionality around the DOM element.
Similarly, $enum()
gives you a wrapper around a raw enum to expose additional/simplified functionality around the enum.
FAQs
TypeScript Enum Utilities
We found that ts-enum-util demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.