How to create a Map in TypeScript

In TypeScript, a Map is a collection of key-value pairs where each key and value can have different types. Maps are useful for associating data and are part of the ECMAScript 6 (ES6) standard.

Building a Basic Map

const userScores = new Map(); userScores.set("John", 100); userScores.set("Alice", 80); userScores.set("Bob", 95); console.log(userScores.size); // Outputs: 3 console.log(userScores.get("Alice")); // Outputs: 80 userScores.delete("Bob"); console.log(userScores.has("Bob")); // Outputs: false

This example showcases creating a map with the Map constructor, setting key-value pairs using set, retrieving values with get, checking size with size, and deleting entries with delete. Notice how keys can be any type like strings, while values can be any data type.

Practical Applications of Maps

  1. Storing user data: Maps can efficiently store user information like names, IDs, and preferences, offering easy access with unique keys.
  2. Caching data: Frequently accessed data can be stored in maps for faster retrieval, improving performance and reducing unnecessary recalculations.
  3. Configuration management: Application settings, environment variables, and other configuration data can be organized and accessed seamlessly using maps.

Iterating over Maps

Maps can be easily iterated using loops or built-in methods.

let colorsMap = new Map<number, string>(); colorsMap.set(1, "red"); colorsMap.set(2, "green"); colorsMap.set(3, "blue"); // Iterating over keys and values using forEach colorsMap.forEach((value, key) => console.log(key, value)); // Iterating over keys using for...of loop for (const key of colorsMap.keys()) { console.log(key); } // Iterating over values using for...of loop for (const value of colorsMap.values()) { console.log(value); }

The forEach method and for...of loops are used to iterate over keys and values in the Map.

Using Objects as Keys

Maps allow objects to be used as keys, offering more flexibility than regular JavaScript objects.

let userRoles = new Map<object, string>(); let user1 = { id: 1, name: "Alice" }; let user2 = { id: 2, name: "Bob" }; userRoles.set(user1, "Admin"); userRoles.set(user2, "User"); console.log(userRoles.get(user1)); // Outputs: "Admin"

In this example, objects (user1 and user2) are used as keys in the Map, associating them with specific roles.

Using Map for Default Values

Maps can be used to provide default values for keys that do not exist.

let preferences = new Map<string, string>(); console.log(preferences.get("theme") "default-theme"); // Outputs: "default-theme" preferences.set("theme", "dark-theme"); console.log(preferences.get("theme") "default-theme"); // Outputs: "dark-theme"

The get method is used to retrieve values, and the || operator provides a default value if the key is not present.

Beyond Basic Maps

  1. TypeScript allows defining custom key equality checks using the equals function, enabling maps to work with complex key types.
  2. Maps can be nested within other maps, creating a hierarchical structure for organizing data at different levels.
  3. Maps can be used to implement efficient object-like structures with dynamic key creation and access, sometimes offering advantages over traditional JavaScript objects.

Conclusion

Maps in TypeScript provide a flexible and efficient way to manage key-value pairs with diverse types. They are suitable for scenarios where the relationships between keys and values are essential. TypeScript's type system enhances the safety and expressiveness of working with Maps.