2024-08-15
1785
#typescript
Kealan Parr
121310
Aug 15, 2024 ⋅ 6 min read

How to extend enums in TypeScript

Kealan Parr Software engineer, technical writer and member of the Unicode Consortium.

Recent posts:

A Guide To The React UseReducer Hook

A guide to the React useReducer Hook

The useReducer React Hook is a good alternative to tools like Redux, Recoil, or MobX.

Ejiro Asiuwhu
Oct 10, 2024 ⋅ 13 min read
Using The Built-In SQLite Module In Node.js

Using the built-in SQLite module in Node.js

Node.js v22.5.0 introduced a native SQLite module, which is is similar to what other JavaScript runtimes like Deno and Bun already have.

Amazing Enyichi Agu
Oct 10, 2024 ⋅ 12 min read
Understanding and supporting zoom behaviors on the web

Understanding and supporting zoom behaviors on the web

Understanding and supporting pinch, text, and browser zoom significantly enhances the user experience. Let’s explore a few ways to do so.

Fimber Elemuwa
Oct 9, 2024 ⋅ 7 min read
Comedy and tragedy masks symbolizing Playwright Extra's ability to extend Playwright with customizable plugins for both stealth and interactive browser automation tasks.

Playwright Extra: extending Playwright with plugins

Playwright is a popular framework for automating and testing web applications across multiple browsers in JavaScript, Python, Java, and C#. […]

Antonello Zanini
Oct 8, 2024 ⋅ 9 min read
View all posts

2 Replies to "How to extend enums in TypeScript"

  1. >In the above code block, we used an intersection type. The intersection acts like an “or,” which simply means that the DoorState type will either be of type Door or of type DoorFrame.

    Isn’t that a union type? Although, the resultant type will let code only compile if the type is used such that only properties in the *intersection* of the types being unioned is accessed.

  2. > Can you extend enums?
    > The short answer is no, you can’t extend enums because TypeScript offers no language feature to > extend them.

    Depends on what you mean by “extend”… but either way, this sentence is pretty falsy. You might not be able to use an `extends` heritage clause as you would with interfaces or classes, but enums are subject to declaration merging in the same manner as namespaces (both `namespace` and the legacy `module` keyword), and interfaces/classes (at the type-level).

    You can also extend the functionality of an enum with static methods, in the same way you would by defining a namespace with the same name as an existing class or function to add types or static methods/properties.

    Here’s a brief real world example:
    “`ts
    // svg path segment commands
    enum Command {
    MoveToAbs = “M”,
    MoveToRel = “m”,
    LineToAbs = “L”,
    LineToRel = “l”,
    // …
    }

    namespace Command {
    // adding types, available as `Command.Absolute` or `Command.Relative`:
    export type Absolute = Command.MoveToAbs | Command.LineToAbs;
    export type Relative = Command.MoveToRel | Command.LineToRel;

    // add runtime functionality with type guards
    export function isAbsolute(it: unknown): it is Absolute {
    return it === Command.MoveToAbs || it === Command.LineToAbs;
    }

    export function isRelative(it: unknown): it is Relative {
    return it === Command.MoveToRel || it === Command.LineToRel;
    }
    }
    “`

Leave a Reply