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:

Solid Principles For Javascript

SOLID principles for JavaScript

SOLID principles help us keep code flexible. In this article, we’ll examine all of those principles and their implementation using JavaScript.

Frank Joseph
Dec 5, 2024 ⋅ 10 min read
Master JavaScript Date And Time: From Moment.js To Temporal

Master JavaScript date and time: From Moment.js to Temporal

JavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.

Yan Sun
Dec 4, 2024 ⋅ 9 min read
Npm Vs. Npx: What’s The Difference?

npm vs. npx: What’s the difference?

Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.

Fimber Elemuwa
Dec 3, 2024 ⋅ 5 min read
How To Audit And Validate AI-Generated Code Output

How to audit and validate AI-generated code output

Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.

Boemo Mmopelwa
Dec 2, 2024 ⋅ 5 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