TypeScript String Enums

1.0.0 · deprecated · verified Sun Apr 19

The `typescript-string-enums` library, currently at stable version 1.0.0, provided a mechanism for creating typesafe string enums in TypeScript environments prior to version 2.4. Its primary utility was to overcome limitations of native TypeScript number enums and the lack of native string enums, offering compile-time type safety for string values. Key features include the `Enum` factory function to define enums with either direct string values or mapped key-value pairs, helper functions like `Enum.isType()` for type guarding, and `Enum.keys()` and `Enum.values()` for strictly typed access to enum keys and values. The library also supports JSDoc comments for enum members and allows creating enums where keys are automatically mirrored as values using `Enum.ofKeys()`. While the library had a steady release cadence up to 0.3.x, development has largely ceased, with the 1.0.0 release being a commitment to stability rather than new features. This library is now largely superseded by native string enums introduced in TypeScript 2.4, which offer similar functionality without a third-party dependency. It is recommended to use native string enums in modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a string enum with mapped values, accessing enum members, using JSDoc, and leveraging `Enum.isType` for type-safe validation and narrowing. Also shows `Enum.keys()` and `Enum.values()`.

import { Enum } from "typescript-string-enums";

// Define an enum with mapped values and JSDoc comments
export const StatusCode = Enum({
  /**
   * The operation completed successfully.
   */
  OK: "success",
  /**
   * An error occurred during the operation.
   */
  ERROR: "failure",
  PENDING: "in_progress",
});
// Create a type alias for compile-time type safety
export type StatusCode = Enum<typeof StatusCode>;

console.log(`OK status value: ${StatusCode.OK}`); // Expected: "success"

function processStatus(statusString: string) {
  // Use Enum.isType as a type guard for runtime validation and type narrowing
  if (Enum.isType(StatusCode, statusString)) {
    // Inside this block, statusString is narrowed to StatusCode type
    console.log(`Processing valid status: ${statusString}`);
    if (statusString === StatusCode.OK) {
      console.log("Operation was successful!");
    } else if (statusString === StatusCode.ERROR) {
      console.log("Operation failed.");
    }
  } else {
    console.warn(`Invalid status received: ${statusString}`);
  }
}

// Example usage with valid and invalid inputs
processStatus("success"); // Valid
processStatus("in_progress"); // Valid
processStatus("unknown_status"); // Invalid

// Demonstrating Enum.keys() and Enum.values()
const keys = Enum.keys(StatusCode);
console.log("Enum keys:", keys); // Expected: ["OK", "ERROR", "PENDING"]

const values = Enum.values(StatusCode);
console.log("Enum values:", values); // Expected: ["success", "failure", "in_progress"]

view raw JSON →