Roadmap

    Let's move on to the next Object Type

    Enum

    Enums (short for "enumerations") in TypeScript are a way to define a set of named constants.

    They help organize related values and make code more readable by providing meaningful names to values.

    Here is an example of a numeric enum in TypeScript:

    ts
    enum Direction {
    Up = 1,
    Down,
    Left,
    Right,
    }

    Above, we have a numeric enum where Up is initialized with 1. All of the following members are auto-incremented from that point on.

    So, Direction.Up has the value 1Down has 2Left has 3, and Right has 4.

    By default, TypeScript enums are numeric.

    The first member gets the value 0, and the values increase by 1 for each subsequent member.

    ts
    enum Colors {
    Red, // 0
    Green, // 1
    Blue, // 2
    }

    We left off the initializer for Red, so it has the value 0 and the rest of the colors are auto-incremented from there.

    You might see this being used when handling error codes in API requests

    ts
    enum ClientErrorCode {
    BadRequest = 400,
    Unauthorized, //401
    PaymentRequired, //402
    Forbidden, //403
    NotFound, //404
    }

    Remember, you can assign non-sequential custom values to enum members.

    ts
    enum Status {
    Success = 1,
    InProgress = 3,
    Failed = 5,
    }

    TypeScript supports both numeric and string enums.

    ts
    // An example of a string enum in TypeScript:
    enum ErrorCode {
    NotFound = "404",
    Unauthorized = "401",
    InternalServerError = "500",
    }
     
    let errorCode: ErrorCode = ErrorCode.NotFound;
    console.log(errorCode); // Output: 404

    Heterogeneous Enums TypeScript also allows enums to contain both string and numeric members, though this is less commonly used.

    Benefits of Using Enums

    • Readability: Enums provide meaningful names to numeric or string values, making code easier to understand.
    • Maintainability: Enums help manage related constants in a centralized way, reducing the risk of errors.
    • Type Safety: TypeScript checks that you use valid enum values, catching errors at compile time.

    Using Enums in Functions

    Enums can be used in function parameters to restrict possible values.

    Example:

    ts
    enum Status {
    Success = 1,
    InProgress = 3,
    Failed = 5,
    }
     
    function respond(status: Status): void {
    if (status === Status.Success) {
    console.log("Operation was successful.");
    } else if (status === Status.InProgress) {
    console.log("Operation is in progress.");
    } else if (status === Status.Failed) {
    console.log("Operation failed.");
    }
    }
     
    respond(Status.Success); // Operation was successful.

    In this example, the respond function only accepts Status enum values, ensuring type safety and clarity.