TypeScript Enums, Tuples & Advanced Objects: A Practical Guide
Sep 2,2026

Series: TypeScript from Basics to Advanced
Part: 8 — Enums, Tuples & Advanced Objects, Index Signatures
Level: Beginner → Intermediate


Introduction

So far in our TypeScript series, we've learned how to work with:

  • Basic types

  • Functions

  • Arrays

  • Objects

  • Type aliases

  • Interfaces

  • Union types

  • Intersection types

  • Type narrowing

  • Type guards

Now we're ready to explore some more powerful ways of describing structured data.

In real-world applications, you'll often encounter situations like:

  • A value must be one of a fixed set of named options.

  • An array must contain values in a specific order and type.

  • An object has dynamic property names.

  • An object has known properties plus additional dynamic properties.

  • API data contains keys that aren't known ahead of time.

  • You need to represent structured key-value data.

TypeScript provides several features for these situations.

In this article, we'll learn:

  • What Enums are

  • Numeric Enums

  • String Enums

  • const enum

  • Enum vs Union Types

  • What Tuples are

  • Optional Tuple Elements

  • Rest Elements in Tuples

  • Readonly Tuples

  • Named Tuple Elements

  • Advanced Object Types

  • Index Signatures

  • Dynamic Object Keys

  • Record

  • Combining known and dynamic properties

  • Real-world API examples

  • Common mistakes

  • Best practices


1. What Is an Enum?

An Enum is a TypeScript feature that allows you to define a set of named constants.

For example:

enum Role {
  Admin,
  User,
  Moderator
}

Now we can use:

const userRole: Role = Role.Admin;

Instead of using arbitrary numbers or strings throughout our application, we have meaningful names.


2. Numeric Enums

By default, TypeScript enums use numbers.

enum Status {
  Pending,
  Success,
  Failed
}

The values are automatically assigned:

Pending → 0
Success → 1
Failed  → 2

So:

console.log(Status.Pending);

produces:

And:

console.log(Status.Success);

produces:


3. Assigning Enum Values Manually

You can specify values yourself.

enum Status {
  Pending = 1,
  Success = 2,
  Failed = 3
}

Now:

Status.Pending; // 1
Status.Success; // 2
Status.Failed;  // 3

You don't have to start at 0.


4. Enum Auto-Incrementing

When you provide one numeric value, subsequent members can continue automatically.

enum Status {
  Pending = 1,
  Success,
  Failed
}

TypeScript assigns:

Pending → 1
Success → 2
Failed  → 3

This can be convenient, but explicit values can sometimes make the intended representation clearer.


5. String Enums

String enums assign explicit string values to each member.

enum Role {
  Admin = "ADMIN",
  User = "USER",
  Moderator = "MODERATOR"
}

Now:

const role: Role = Role.Admin;

The runtime value is:

"ADMIN"

String enums are often easier to understand when inspecting API payloads or debugging application state.


6. Practical Enum Example

Suppose we're building an online test application.

We could define:

enum TestStatus {
  NotStarted = "NOT_STARTED",
  InProgress = "IN_PROGRESS",
  Completed = "COMPLETED"
}

Then:

interface Test {
  id: string;
  title: string;
  status: TestStatus;
}

Now:

const test: Test = {
  id: "TEST001",
  title: "JavaScript Assessment",
  status: TestStatus.InProgress
};

This prevents arbitrary status values from being used.


7. Enums with Functions

Enums can also be used with functions.

enum Role {
  Admin = "ADMIN",
  User = "USER"
}

function checkAccess(role: Role) {
  if (role === Role.Admin) {
    console.log("Full access");
  } else {
    console.log("Limited access");
  }
}

Now:

checkAccess(Role.Admin);

is valid.


8. Enum Reverse Mapping

Numeric enums have an additional runtime behavior called reverse mapping.

For example:

enum Status {
  Pending,
  Success,
  Failed
}

You can access:

Status.Pending;

and also:

Status[0];

For numeric enums:

Status.Pending → 0
Status[0]      → "Pending"

String enums do not provide this same numeric reverse mapping.


9. const enum

TypeScript also supports const enum.

const enum Direction {
  Up,
  Down,
  Left,
  Right
}

These can be inlined during compilation in configurations that preserve the relevant behavior.

For most application code, however, you should understand regular enums first and use const enum deliberately rather than automatically.


10. Enum vs Union Type

You may notice that we can represent the same fixed set of values using a union.

For example:

type Role =
  | "admin"
  | "user"
  | "moderator";

Or:

enum Role {
  Admin = "admin",
  User = "user",
  Moderator = "moderator"
}

Both can restrict values.

A literal union is often simpler when you only need a compile-time set of allowed values:

type Role = "admin" | "user" | "moderator";

An enum is useful when you specifically want named members and are comfortable with its runtime representation.

For modern TypeScript applications, especially when working closely with JSON APIs, literal unions are often a very good default.


11. What Is a Tuple?

A Tuple is an array with a fixed number of elements where each position has a specific type.

For example:

let user: [string, number];

user = ["Sundar", 30];

The first element must be:

string

The second element must be:

number

This is different from:

let values: (string | number)[];

because an ordinary union array doesn't enforce the position of each value.


12. Tuple vs Array

Consider:

let tuple: [string, number];

tuple = ["Sundar", 30];

This is valid.

But:

tuple = [30, "Sundar"];

is invalid because the order is wrong.

With:

let array: (string | number)[];

array = [30, "Sundar"];

the order doesn't matter.

So:

Array
→ Collection of values

Tuple
→ Fixed structure with positional types

13. Accessing Tuple Elements

Consider:

const user: [string, number] = [
  "Sundar",
  30
];

TypeScript knows:

user[0]

is a:

string

And:

user[1]

is a:

number

This gives us better type safety than a generic array.


14. Named Tuple Elements

You can give tuple elements meaningful names.

type User = [
  name: string,
  age: number
];

Now:

const user: User = [
  "Sundar",
  30
];

The names are mainly for readability and tooling; they don't change the underlying tuple structure.


15. Optional Tuple Elements

Tuple elements can be optional.

type User = [
  string,
  number,
  string?
];

Now both are valid:

const user1: User = [
  "Sundar",
  30
];

and:

const user2: User = [
  "Sundar",
  30,
  "India"
];

The third element is optional.


16. Rest Elements in Tuples

Tuples can also contain rest elements.

type Numbers = [
  string,
  ...number[]
];

This means:

First element → string
Remaining elements → numbers

For example:

const values: Numbers = [
  "Scores",
  10,
  20,
  30,
  40
];

This is useful when the first element has a special meaning and the remaining values follow a pattern.


17. Readonly Tuples

You can prevent tuple modification with readonly.

const user: readonly [string, number] = [
  "Sundar",
  30
];

Now:

user[0] = "Rao";

is not allowed.

Readonly tuples are useful when data should not be modified after creation.


18. Practical Tuple Example

Tuples are useful when a function needs to return multiple values with known positions.

For example:

function getUser(): [string, number] {
  return ["Sundar", 30];
}

Now:

const [name, age] = getUser();

TypeScript knows:

name → string
age  → number

19. Tuples with API Data

Suppose an API returns coordinates:

type Coordinates = [number, number];

const location: Coordinates = [
  17.3850,
  78.4867
];

The first value can represent latitude and the second longitude.

A tuple communicates that this isn't just an arbitrary number array; the positions have meaning.


20. Advanced Object Types

We've already seen basic object types:

type User = {
  name: string;
  age: number;
};

But TypeScript allows us to model more complex objects.

For example:

type User = {
  id: string;
  profile: {
    name: string;
    email: string;
  };
  roles: string[];
};

Now:

const user: User = {
  id: "USR001",
  profile: {
    name: "Sundar",
    email: "sundar@example.com"
  },
  roles: ["developer", "admin"]
};

TypeScript checks every nested property.


21. Optional Object Properties

Properties can be optional using ?.

type User = {
  name: string;
  email: string;
  phone?: string;
};

Now this is valid:

const user: User = {
  name: "Sundar",
  email: "sundar@example.com"
};

And this is also valid:

const user: User = {
  name: "Sundar",
  email: "sundar@example.com",
  phone: "9999999999"
};

22. Readonly Object Properties

You can prevent reassignment of a property using readonly.

type User = {
  readonly id: string;
  name: string;
};

Now:

const user: User = {
  id: "USR001",
  name: "Sundar"
};

This is allowed:

user.name = "Rao";

But this is not:

user.id = "USR002";

23. Nested Readonly Properties

Readonly can also be used with nested objects.

type User = {
  readonly id: string;

  profile: {
    readonly email: string;
    name: string;
  };
};

Now:

user.profile.name = "Rao";

is allowed.

But:

user.profile.email = "new@example.com";

is not allowed.


24. Index Signatures

Now let's look at one of the most useful advanced object features.

An Index Signature allows an object to have dynamic property names.

Suppose we don't know the property names in advance.

For example:

type Scores = {
  [studentName: string]: number;
};

Now we can have:

const scores: Scores = {
  Sundar: 90,
  Rahul: 85,
  Priya: 95
};

The keys are dynamic.

But every value must be a number.


25. Understanding Index Signature Syntax

The syntax is:

[key: string]: number;

The name key is just a variable name.

You could also write:

[name: string]: number;

or:

[student: string]: number;

They mean the same thing.

The important part is:

[string]: number

which means:

Any string key must have a number value.


26. Index Signature with Boolean Values

You can use other value types.

type FeatureFlags = {
  [feature: string]: boolean;
};

Now:

const features: FeatureFlags = {
  darkMode: true,
  notifications: false,
  analytics: true
};

This is useful for configuration objects and feature flags.


27. Index Signature with String Values

For example:

type Environment = {
  [key: string]: string;
};

Now:

const env: Environment = {
  API_URL: "https://api.example.com",
  NODE_ENV: "production",
  VERSION: "1.0.0"
};

Every value must be a string.


28. Index Signatures with Known Properties

You can combine known properties with dynamic properties, but the known properties must be compatible with the index signature.

For example:

interface UserScores {
  [username: string]: number;
  Sundar: number;
  Rahul: number;
}

This is valid because Sundar and Rahul also have number values.

But this would be problematic:

interface UserScores {
  [username: string]: number;
  admin: string;
}

Why?

Because admin is also a string key, so the index signature says its value must be a number.

The types conflict.


29. Number Index Signatures

Index signatures can also use numeric keys.

type Scores = {
  [index: number]: string;
};

Now:

const scores: Scores = {
  0: "Excellent",
  1: "Good",
  2: "Average"
};

This can be useful when modeling number-indexed structures.


30. Record Type

TypeScript also provides a utility type called Record.

For example:

type Scores = Record<string, number>;

This is similar to:

type Scores = {
  [key: string]: number;
};

Now:

const scores: Scores = {
  Sundar: 90,
  Rahul: 85
};

Record is often convenient when you want to describe a dictionary-like object.


31. Record with Specific Keys

Record becomes particularly useful when the keys themselves are known.

type Role = "admin" | "user" | "moderator";

type Permissions = Record<Role, string[]>;

Now the object must contain all three keys:

const permissions: Permissions = {
  admin: ["read", "write", "delete"],
  user: ["read"],
  moderator: ["read", "write"]
};

This is a powerful way to ensure that a configuration object contains exactly the expected set of keys.


32. Dynamic API Response Example

Imagine an API returns user statistics where the usernames are dynamic.

interface UserStats {
  [username: string]: {
    score: number;
    completedTests: number;
  };
}

Now:

const stats: UserStats = {
  Sundar: {
    score: 95,
    completedTests: 10
  },

  Rahul: {
    score: 88,
    completedTests: 8
  }
};

We don't need to know the usernames ahead of time.

TypeScript still validates the structure of every value.


33. Advanced Object: Nested Dynamic Keys

You can combine index signatures with nested objects.

type TestResults = {
  [student: string]: {
    [subject: string]: number;
  };
};

Now:

const results: TestResults = {
  Sundar: {
    JavaScript: 90,
    TypeScript: 95,
    React: 88
  },

  Rahul: {
    JavaScript: 85,
    TypeScript: 89,
    React: 92
  }
};

This allows both:

Dynamic students
      ↓
Dynamic subjects
      ↓
Numeric scores

34. Combining Index Signatures with Optional Properties

You can also create flexible objects.

For example:

interface Config {
  name: string;
  [key: string]: string;
}

Now:

const config: Config = {
  name: "production",
  region: "india",
  version: "1.0"
};

The known property name and all dynamic properties must have compatible value types.


35. Real-World Configuration Example

Index signatures are useful for environment-style configuration.

interface AppConfig {
  [key: string]: string;
}

const config: AppConfig = {
  API_URL: "https://api.example.com",
  API_VERSION: "v1",
  ENVIRONMENT: "production",
  APP_NAME: "Online Test"
};

Now TypeScript ensures that every configuration value is a string.


36. Index Signature with Union Values

Sometimes dynamic properties can have more than one type.

interface Metadata {
  [key: string]: string | number | boolean;
}

Now:

const metadata: Metadata = {
  name: "Sundar",
  age: 30,
  verified: true
};

Each value can be:

string
OR
number
OR
boolean

This is useful for flexible metadata objects.

However, avoid making types broader than necessary. A more precise model is usually easier to work with.


37. Index Signatures vs any

Avoid:

const data: any = {};

when you know something about the structure.

Instead:

interface Data {
  [key: string]: string | number;
}

Now TypeScript still provides useful checking.


38. Advanced Object Example

Let's combine several concepts.

type TestStatus =
  | "not-started"
  | "in-progress"
  | "completed";

interface Test {
  readonly id: string;
  title: string;
  status: TestStatus;
  metadata?: {
    [key: string]: string | number | boolean;
  };
}

Now:

const test: Test = {
  id: "TEST001",
  title: "JavaScript Assessment",
  status: "in-progress",
  metadata: {
    difficulty: "medium",
    duration: 60,
    randomQuestions: true
  }
};

Notice how several TypeScript features work together:

  • readonly

  • Union types

  • Optional properties

  • Nested objects

  • Index signatures

  • Multiple value types

This is the kind of type composition you'll frequently see in real applications.


39. When Should You Use Tuples?

Tuples are useful when:

  • The number of elements is known.

  • The order of elements matters.

  • Each position has a different meaning.

  • A function returns multiple values.

  • You're modeling coordinate-like data.

  • You want positional type safety.

For example:

type Response = [
  number,
  string
];

40. When Should You Use Index Signatures?

Index signatures are useful when:

  • Property names are dynamic.

  • You don't know all keys in advance.

  • You're modeling dictionaries.

  • You're working with dynamic API responses.

  • You're creating configuration objects.

  • You're modeling feature flags.

  • You're storing dynamic metadata.

For example:

type FeatureFlags = {
  [key: string]: boolean;
};

41. When Should You Use Record?

Use Record when you want a concise way to represent an object whose keys and values follow a known type relationship.

For example:

type Scores = Record<string, number>;

Or when a fixed set of keys must all exist:

type Role = "admin" | "user" | "moderator";

type Permissions = Record<Role, string[]>;

42. Common Mistakes

Mistake 1: Using enums for everything

Don't automatically create an enum whenever you have a few fixed values.

For example:

type Status =
  | "pending"
  | "success"
  | "failed";

may be simpler than:

enum Status {
  Pending = "pending",
  Success = "success",
  Failed = "failed"
}

Choose based on whether you need enum members and their runtime representation.


Mistake 2: Using tuples when an object is clearer

This:

type User = [string, number, string];

can be difficult to understand.

An object may be clearer:

type User = {
  name: string;
  age: number;
  email: string;
};

If the positions don't have a strong reason to matter, an object is often easier to maintain.


Mistake 3: Making index signatures too broad

Avoid unnecessarily broad types:

[key: string]: any;

Prefer:

[key: string]: string | number;

when those are the actual possible values.


Mistake 4: Conflicting known properties

This is invalid:

interface Data {
  [key: string]: number;
  name: string;
}

Because name is also a string key, but its value is not a number.


Mistake 5: Forgetting readonly behavior

Remember that:

readonly

prevents reassignment through that property in TypeScript.

It doesn't automatically make an entire object deeply immutable.


43. Best Practices

1. Prefer simple types when possible

Don't use an enum, tuple, or index signature just because TypeScript supports it.

Use the simplest type that accurately models your data.

2. Use tuples for genuinely positional data

If the position has meaning, tuples are appropriate.

3. Use objects for named data

If values have meaningful names, an object is usually clearer.

4. Keep index signatures precise

Instead of:

[key: string]: any;

prefer:

[key: string]: string | number;

when possible.

5. Consider Record for dictionary-like structures

Record<string, number>

is concise and readable.

6. Use discriminated unions for application states

For example:

type State =
  | { status: "loading" }
  | { status: "success"; data: User }
  | { status: "error"; message: string };

This works especially well with the type narrowing concepts from Part 7.


Final Takeaway

In this part, we explored several powerful TypeScript features for modeling structured data.

Enums allow you to define named sets of constants:

enum Role {
  Admin = "admin",
  User = "user"
}

Tuples allow you to define fixed positional structures:

type User = [string, number];

Index signatures allow dynamic object keys:

type Scores = {
  [student: string]: number;
};

And Record provides a concise way to create dictionary-like objects:

type Scores = Record<string, number>;

The important thing is not just knowing the syntax.

You should understand why each feature exists:

  • Use enums when named runtime members are useful.

  • Use literal unions when you simply need a restricted set of values.

  • Use tuples when position matters.

  • Use objects when properties have meaningful names.

  • Use index signatures when keys are dynamic.

  • Use Record when you want a concise key-value object type.

These features become particularly useful when building large React, Node.js, and full-stack TypeScript applications.