Typescript String Enums Versions Save

Typesafe string enums in TypeScript pre-2.4.


5 years ago

No changes, but committing to a stable release.


7 years ago

Fixes sourcemaps by inlining sources.


7 years ago

Introduces Enum.ofKeys function. As described in the README:


Creates a new enum with the same keys as the provided enum or object and whose values are equal to its keys. This is most useful if for some reason it is necessary to do string comparisons against the keys of an enum rather than the values. For example:

const ErrorColor = Enum({ OK: "green", ERROR: "red" });
type ErrorColor = Enum<typeof ErrorColor>;

const ErrorLevel = Enum.ofKeys(ErrorColor);

const errorLevel = getErrorLevel();

if (errorLevel === ErrorLevel.ERROR) {


7 years ago

Adds Enum.isType(), a type-checking function that can be used as a guard.

Sample usage:

const Color = Enum("RED", "GREEN", "BLUE", "PUCE");
type Color = Enum<typeof Color>;

let selectedColor: Color;

const colorString = getUserInputString(); // Unsanitized string.
if (Enum.isType(Color, colorString)) {
    // Allowed because within type guard.
    selectedColor = colorString;
} else {
    throw new Error(`${colorString} is not a valid color`);


7 years ago

Introduce Enum.keys() and Enum.values(), which resemble Object.keys() and Object.values() but provide strict typing in their return type:

const FileType = Enum({
  PDF: "application/pdf",
  Text: "text/plain",
  JPEG: "image/jpeg",
type FileType = Enum<typeof FileType>;

const keys = Enum.keys(FileType);
// Inferred type: ("PDF" | "Text" | "JPEG")[]
// Return value: ["PDF", "Text", "JPEG"] (not necessarily in that order)

const values = Enum.values(FileType);
// Inferred type: ("application/pdf" | "text/plain" | "image/jpeg")[]
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)

Thanks again to @kourge for his contribution!

(This release fixes a publishing issue in 0.3.0)


7 years ago

The artifact for this version is broken on npm. Please use 0.3.1 or later instead.


7 years ago

Enums can now be created with keys that differ from their string values. Additionally, these entries may have JSDoc comments. For example,

export const Status = Enum({
     * Everything is fine.
     * Hovering over Status.RUNNING in an IDE will show this comment.
    RUNNING: "running",

     * All is lost.
    STOPPED: "stopped",
export type Status = Enum<typeof Status>;

console.log(Status.RUNNING); // -> "running"

Thanks to @kourge for contributing!