How to Initialize an Empty Dictionary in TypeScript (with Practical Examples)

One of my team members recently asked about initializing an empty dictionary in TypeScript. It is easy in TypeScript. In this tutorial, I’ll walk you through exactly how to initialize an empty dictionary in TypeScript, with clear, practical examples you can use right away. I will also share some best practices and highlight key tips to help you avoid common mistakes.

What Is a Dictionary in TypeScript?

In TypeScript, a “dictionary” is typically an object or map that lets you store values using dynamic keys—usually strings or numbers. This is perfect for scenarios where you don’t know all your keys ahead of time, like building a lookup table for U.S. states, storing user data by ID, or mapping product SKUs to inventory counts.

Unlike arrays (which use numeric indices), dictionaries use custom keys. TypeScript’s strong typing helps you define exactly what kinds of keys and values your dictionary should accept, making your code safer and more predictable.

Check out Check if a Dictionary is Empty in TypeScript

Now, let me show you different methods to initialize an empty dictionary in TypeScript.

Method 1: Using Index Signatures (Most Common)

The most common way to initialize an empty dictionary in TypeScript is by using an index signature. This lets you define the type of keys and the type of values your dictionary will hold.

Example: Mapping State Abbreviations to State Names

// Define the dictionary type
let states: { [abbr: string]: string } = {};

// Add entries
states["CA"] = "California";
states["NY"] = "New York";

You can see the output in the screenshot below:

Initialize an Empty Dictionary in TypeScript

How it works:

  • { [abbr: string]: string } means “an object with string keys and string values.”
  • We initialize it as an empty object ({}).
  • You can add as many key-value pairs as you need, and TypeScript will enforce that both keys and values are strings.

When to Use This Method

This method is perfect when you want a simple, flexible dictionary and don’t need advanced features like key ordering or built-in methods for iteration.

Read Get First Element from a Typescript Dictionary

Method 2: Using the Record Utility Type (Clean and Type-Safe)

TypeScript offers a built-in utility type called Record that makes dictionary definitions even cleaner. Let me show you an example to know how to initialize an empty dictionary in TypeScript.

Example: Mapping ZIP Codes to City Names

// Using Record utility type
let zipToCity: Record<string, string> = {};

// Add entries
zipToCity["90210"] = "Beverly Hills";
zipToCity["10001"] = "New York";

Why use Record?

  • Record<string, string> means “an object with string keys and string values.”
  • It’s concise and easy to read.
  • TypeScript will ensure you only use the defined types for keys and values.

Restricting Keys with Record

If you know all possible keys in advance (for example, the 50 U.S. state abbreviations), you can restrict the dictionary to just those keys for extra type safety.

type StateAbbreviation = "CA" | "NY" | "TX" | "FL"; // etc.

let stateNames: Record<StateAbbreviation, string> = {
  CA: "California",
  NY: "New York",
  TX: "Texas",
  FL: "Florida"
  // ...add all states as needed
};

Read Check If Value Exists In Dictionary In TypeScript

Method 3: Using Map for Advanced Use Cases

If you need more advanced features—like preserving key insertion order, or using non-string keys—TypeScript’s Map class is a great option.

Example: Mapping User IDs to User Objects

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

let userMap = new Map<number, User>();

// Add entries
userMap.set(101, { id: 101, name: "Alice", email: "alice@example.com" });
userMap.set(202, { id: 202, name: "Bob", email: "bob@example.com" });

// Retrieve a user
let user = userMap.get(101);

When to use Map over plain objects?

  • When you need non-string keys (like numbers or objects).
  • When you care about the order of entries.
  • When you want built-in methods like .set().get().has(), and .delete().

Check out How to Sort a TypeScript Dictionary by Value?

Method 4: Defining a Dictionary Interface (For Reusability)

If you’ll use the same dictionary structure in multiple places, define an interface for clarity and reusability.

Example: Tracking Product Inventory by SKU

interface Inventory {
  [sku: string]: number; // SKU as key, quantity as value
}

let inventory: Inventory = {};

// Add items
inventory["SKU123"] = 50;
inventory["SKU456"] = 120;

This approach makes your code self-documenting and easier to maintain, especially in larger projects.

Read How to Convert a Dictionary to an Array in Typescript?

Best Practices and Tips

Here are some best practices and tips you should follow while initializing an empty dictionary in TypeScript.

  1. Always Type Your Dictionaries: TypeScript’s real power is in its type safety. Always define the key and value types for your dictionaries to catch errors early and get better IntelliSense in your editor.
  2. Prefer Record for Simplicity: For most use cases, Record<string, Type> is concise, readable, and easy to maintain. If you need more flexibility, use index signatures or interfaces.
  3. Use Map for Complex Keys: If your keys are not strings (like numbers or objects), or you need features like ordering, use Map.
  4. Avoid Using Arrays for Dictionary-Like Data: Arrays are great for ordered lists, but they’re not designed for dynamic key-value storage. Stick with objects, Record, or Map for dictionaries.

Common Mistakes to Avoid

Here are some of the common mistakes you should avoid while initializing an empty dictionary.

  • Forgetting to type your dictionary:
    If you just use {} without a type, TypeScript won’t catch mistakes like mixing value types or using the wrong keys.
  • Using arrays for key-value storage:
    Arrays are indexed by numbers, not strings. Using them for dictionary-like data can lead to bugs.
  • Assuming all keys are present:
    When accessing a value by key, always check if it exists to avoid runtime errors.

Conclusion

In this tutorial, I explained how to initialize an empty dictionary in TypeScript using several practical methods, including index signatures, the Record utility type, Map, and custom interfaces. With these approaches, you can easily and safely manage dynamic key-value pairs in your TypeScript projects.

You may also like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App