How to Sort by Enum in TypeScript (Numeric and String Enums)

Sort by enum in TypeScript: Command Prompt output of orders sorted alphabetically and by a Record order map

To sort by enum in TypeScript, compare the enum members inside sort(). A numeric enum is already a number, so a.priority – b.priority works. A string enum needs a rank map typed as Record<Enum, number>: Every snippet here ran in strict mode on TypeScript 7.0.2 and Node.js v22.22.2. The output blocks are pasted straight from … Read more >>

How to Convert a TypeScript Enum to a Number (With Examples)

Convert TypeScript enum to number: Command Prompt output of a numeric enum read as a number

The simplest way to convert TypeScript enum to number values is to read the member itself: a numeric enum member already is a number at runtime, so const n: number = OrderStatus.Shipped works with no cast. String enums are different, and need a lookup map: Nothing below is hand-typed output: I ran each file through … Read more >>

How to Convert a String to an Enum in TypeScript

Convert string to enum in TypeScript: Command Prompt output of a type guard parsing order statuses

To convert a string to an enum in TypeScript, check the string against the enum’s values with a type guard, then return the enum member or undefined. A plain as cast checks nothing: My test setup for this post: TypeScript 7.0.2 and Node.js v22.22.2, strict mode on, and nothing in the outputs below is edited … Read more >>

TypeScript Enum with Multiple Values (4 Patterns That Work)

TypeScript enum with multiple values: Command Prompt output of a Record lookup giving each enum member a label, price and delivery days

A TypeScript enum with multiple values isn’t something the language supports directly: each member holds exactly one number or string. The clean fix is to keep the enum for the name and attach the extra values with a typed lookup object, Record<YourEnum, Details>: My test bench for this one was TypeScript 7.0.2 and Node.js v22.22.2 … Read more >>

TypeScript Enum Naming Conventions

Enum naming convention in TypeScript

In this tutorial, I will explain the intricacies of TypeScript enum naming conventions. By the end of this post, you’ll understand how to name your enums effectively, making your code more readable and maintainable. Enums, short for enumerations, are a feature in TypeScript that allows you to define a set of named constants. These constants … Read more >>

TypeScript Enum Reverse Mapping

Reverse Enum Strings in Typescript

While working on a TypeScript project, I got a requirement to map enum values back to their corresponding names, essentially the reverse of how enums are typically used. With numeric enums, TypeScript handles this automatically. But when it came to string enums, I had to implement a custom solution to make reverse mapping work. In … Read more >>

How to Iterate Over Enums in TypeScript?

Iterate Over Enum Keys in TypeScript

When working on TypeScript applications, especially those involving fixed sets of related values like states, cities, or user roles, enums become an essential tool for writing maintainable code. Recently, while working on a project for a US-based company, I encountered a requirement to loop through enum values to display options in a UI dynamically. For … Read more >>

TypeScript Switch Case With Enums

Enums in Switch Case in TypeScript

When working on TypeScript applications, especially in larger projects, using switch statements with enums can significantly enhance the code’s maintainability. Recently, while building features for a web app that involved managing user roles and order statuses, I came to know about the use of enums in handling a fixed set of related values. I learned … Read more >>

How to Get All Enum Values in TypeScript

Get All Enum Values in TypeScript

While working on a TypeScript project, I had an enum that listed different user roles, such as Admin, Editor, and Viewer. I wanted to display all these roles in a drop-down menu on the webpage. To achieve this, I needed a way to retrieve all the values from the enum so I could loop through … Read more >>

Use TypeScript Enums as Object Keys

Use TypeScript Enums as Object Keys

While working on a TypeScript project, I needed to store values in an object where the keys represented different user roles, such as Admin, Editor, and Viewer. Instead of using plain strings (which can cause typos or mistakes), I used a TypeScript enum to define the roles. This made the code more readable, safer, and … Read more >>