This Is How I Mastered TypeScript Like I'm 5 (Enums!)(9)
Karandeep Singh

Karandeep Singh @karandeepsingh7070

About: Always on Builder Mode type of software developer, sharing my interesting findings in the hope that they will be helpful on your coding journey.

Location:
New Delhi
Joined:
Jun 16, 2025

This Is How I Mastered TypeScript Like I'm 5 (Enums!)(9)

Publish Date: Jul 2
10 0

Today! We’re going to continue TypeScript learning like you’re a smart 5-year-old who loves to build things and asks “why?” (which is the best thing ever).

& yes “why?” is my way of learning.

I've divided this into 20 Chapters. and will go one by one and each will be of 2 - 3 min. of read.
This is a Continuation. if you have not read the Previous chapter - Chapter 8

🧩 Chapter 9: Enums

(aka: “Giving numbers and strings meaningful values for your app.”)

🎨 Imagine This:

You’re making a game with four difficulties:

  • Easy
  • Medium
  • Hard
  • Impossible

You could:

  • Use "easy", "medium", etc. everywhere (but risk typos like "medum")
  • Use numbers like 1, 2, 3, 4 (but you’ll forget which is which)

Enums help you give names to these values and keep them organized.


🚦 What is an Enum?

An Enum is like a labeled list of values you can use in your code.

🧪 Basic Enum Example:

enum Difficulty {
  Easy,
  Medium,
  Hard,
  Impossible,
}

let gameLevel: Difficulty = Difficulty.Medium;

console.log(gameLevel); // 1 (because Easy=0, Medium=1, ...)
Enter fullscreen mode Exit fullscreen mode

🤝 Why use Enums?

Readability: Difficulty.Medium is clearer than "medium" or 1.
Safety: Prevents typos ("medum" ❌).
Organized: All values live in one place.


Production Example: Enums for User Roles in an App

Imagine you’re building a team management SaaS.
Users can have roles like:

  • Admin
  • Editor
  • Viewer

Using Enums, you can cleanly manage permissions.

🚦 Step 1: Define the Enum

// userRoles.ts

export enum UserRole {
  Admin = "ADMIN",
  Editor = "EDITOR",
  Viewer = "VIEWER",
}
Enter fullscreen mode Exit fullscreen mode

🚦 Step 2: Use it in your user model

// models/user.ts

import { UserRole } from "./userRoles";

type User = {
  id: string;
  name: string;
  email: string;
  role: UserRole;
};

const user: User = {
  id: "u123",
  name: "Karandeep Singh",
  email: "karandeep@wisdimbits.com",
  role: UserRole.Editor,
};
Enter fullscreen mode Exit fullscreen mode

🚦 Step 3: Permission checks in services

// services/checkPermission.ts

import { UserRole } from "./userRoles";

function canEditContent(role: UserRole) {
  return role === UserRole.Admin || role === UserRole.Editor;
}

// Usage
if (canEditContent(user.role)) {
  console.log("User can edit content");
} else {
  console.log("User can only view content");
}
Enter fullscreen mode Exit fullscreen mode

🌱 Summary

Using enums for roles, statuses, modes, environment constants, and permission levels is a best practice in production TypeScript apps to:
✅ Reduce bugs
✅ Improve clarity
✅ Enable safe refactoring later.


Read Previous Chapters

If you enjoyed this and want to master TypeScript and other technologies, follow the series and drop a like!🤝

I’m a passionate software developer sharing the cool things I discover, hoping they help you level up on your coding journey.

How i created my own State Management Library : Rethinking State Management in React — From a Dev, for Developers.

Comments 0 total

    Add comment