Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Primitive and Special Types

TypeScript Map and Set types

Reference for Map<K, V> and Set<T> in TypeScript: typing keys and values, iteration, conversion to arrays and objects, and when to prefer them over objects.

Map<K, V> stores key-value pairs where the key can be any type, and Set<T> stores unique values. Both are generic classes, so the element types are part of the type: a Map<string, number> only accepts string keys and number values, and a Set<string> only accepts strings.

Unlike plain objects, a Map keeps insertion order for all key types, has a real size, and has no inherited keys to worry about. A Set removes duplicates using the same-value-zero rule: NaN equals NaN, and 0 equals -0. Objects are compared by identity, not by content.

TypeScript
const stock = new Map<string, number>([["apple", 3]]);
const tags = new Set<string>(["a", "b"]);

Try it

TypeScript Map with typed keys and values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Set operations and object keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
class Map<K, V> {
  constructor(entries?: Iterable<readonly [K, V]> | null);
  get(key: K): V | undefined;
  set(key: K, value: V): this;
  has(key: K): boolean;
  delete(key: K): boolean;
  clear(): void;
  forEach(cb: (value: V, key: K, map: Map<K, V>) => void): void;
  readonly size: number;
  keys(): MapIterator<K>;
  values(): MapIterator<V>;
  entries(): MapIterator<[K, V]>;
}
class Set<T> {
  constructor(values?: Iterable<T> | null);
  add(value: T): this;
  has(value: T): boolean;
  delete(value: T): boolean;
  clear(): void;
  readonly size: number;
}
Name Description
K, V Key and value types of the map. Inferred from the constructor argument when you pass entries.
T Element type of the set.
key, value Must be assignable to K, V or T; anything else is a compile error.

Returns: get returns V | undefined. set and add return the collection itself for chaining. delete returns true if something was removed.

Edge cases: new Map() with no arguments infers Map<any, any>, so always give type arguments for an empty map. new Set([]) infers Set<never>.

Usage notes

  • get() cannot tell "missing" from "stored undefined" when V includes undefined; use has() when that matters.
  • Iteration order is insertion order. Re-setting an existing key keeps its original position.
  • Map and Set are not JSON-serializable: JSON.stringify(new Map()) gives {}. Convert with Object.fromEntries or [...set] first.
  • Array methods are missing. Spread into an array to use filter, map or sort. Newer runtimes (Node 22+) also have Set methods like union and intersection; they need lib to include esnext or a recent target, so the spread pattern is the portable option.
  • Use a Map instead of an object when keys are not known ahead of time, are not strings, or are added and removed often. Use an object or Record for fixed, known keys.
  • ReadonlyMap<K, V> and ReadonlySet<T> remove the mutating methods from the type; they are the right parameter types for functions that only read.
  • Iterating a collection while adding to it can loop forever; copy first if you plan to modify it.
  • WeakMap and WeakSet are the variants that hold object keys without preventing garbage collection.