TypeScript Dictionary: The Ultimate Guide with Practical Examples (2026)

Master the implementation of key-value pair structures in TypeScript 5+. Learn about Record types, Interface index signatures, and ES6 Maps with a focus on type safety and performance.

# What is a Dictionary in TypeScript?

In computer science, a dictionary (also known as a map or associative array) is a data structure composed of a collection of [key, value] pairs, such that each possible key appears at most once in the collection.

In TypeScript, "Dictionary" is not a built-in keyword like in C# or Python. Instead, developers use the flexible type system to define objects that behave like dictionaries. This is primarily achieved through Index Signatures or the built-in Record<K, V> utility type.

Why Dictionaries Matter: They allow for constant-time complexity (O(1)) for access, insertion, and deletion of items when the key is known, making them indispensable for large-scale data processing in the browser and Node.js.

# How to Declare and Initialize a Dictionary in TypeScript

In 2026, the two most common ways to define a dictionary are using Interfaces with index signatures or the Record utility type. Let's compare both.

Using Index Signatures in Interfaces

Index signatures allow you to define the type of the key and the type of the value that an object can contain when the property names are not known ahead of time.

interface UserDictionary {
  [id: string]: { name: string; email: string };
}

const users: UserDictionary = {
  "u101": { name: "John Doe", email: "john@example.com" },
  "u102": { name: "Jane Smith", email: "jane@example.com" }
};

Using the Built-in Record Utility Type

The Record<Keys, Type> utility type is a more concise and modern way to declare a dictionary. It is functionally identical to the interface approach but often preferred for its readability.

type ProductCatalog = Record;

const products: ProductCatalog = {
  100: "MacBook Pro",
  200: "iPhone 15",
  300: "iPad Air"
};

# Advanced Dictionary Implementations: Record vs Interface

Choosing between Record and an interface often depends on the complexity of your keys. While an interface is limited to string or number as index types, Record can work with string literal unions, providing powerful compile-time checks.

type AppTheme = 'light' | 'dark' | 'high-contrast';

const themeColors: Record = {
  'light': '#ffffff',
  'dark': '#333333',
  'high-contrast': '#000000'
};
// TypeScript will error if a theme is missing or misspelled

After setting up your dictionary structures, ensure they are well-documented by following our guide on documenting your TypeScript code with TypeDoc.

# Common Operations: Accessing, Adding, and Deleting Keys

Working with dictionaries involves standard CRUD operations. However, in TypeScript, you must handle the possibility that a key might not exist.

  • Adding/Updating: dict[key] = value;
  • Accessing: const val = dict[key]; (Note: This might be undefined!)
  • Deleting: delete dict[key];
Type Safety Alert: When accessing a dictionary, TypeScript by default assumes the key exists. Enable noUncheckedIndexedAccess in your tsconfig.json to force 'undefined' checks and improve reliability.

# How to Iterate Through a TypeScript Dictionary

Iterating through keys and values correctly is crucial for performance and type safety.

Iterating with Object.entries()

This is the modern way to get both keys and values in a single loop.

Object.entries(users).forEach(([id, user]) => {
  console.log(`User ID: ${id}, Name: ${user.name}`);
});

Iterating with Object.keys()

If you only need the keys, Object.keys() is slightly more performant.

# TypeScript Dictionary vs. Map: When to Use Which?

Feature Object-based (Record/Interface) ES6 Map Object
Key Types String or Symbol only Any type (Objects, Functions, etc.)
Ordering No guaranteed order Maintains insertion order
Performance Faster for small datasets Better for frequent additions/deletes
Serialization Easily JSON.stringify()-able Requires manual conversion

Frequently Asked Questions about TS Dictionaries

Does TypeScript have a native Dictionary type?

No, TypeScript uses index signatures or the Record<K, V> utility type to create dictionary-like structures using standard JavaScript objects.

What is the difference between a Dictionary and a Map in TypeScript?

A Dictionary is usually a plain object with typed keys, while a Map is a built-in ES6 object that allows any type for keys and maintains insertion order.

How to handle dynamic keys in a TypeScript interface?

You can use an index signature: [key: string]: valueType;. This allows the object to accept any string as a property name.