ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Map in TypeScript is a built-in, generic iterable collection that stores key-value pairs. Unlike standard JavaScript objects, a Map permits keys of any data type—including objects, functions, and primitives—and strictly maintains the insertion order of its elements. TypeScript enhances the native JavaScript Map by enforcing strict type safety through generics (Map<K, V>).
Instantiation and Typing
TypeScript utilizes generics to define the types for the keys (K) and values (V). If initialized with values, TypeScript can infer these types, but explicit declaration is standard practice for empty maps.
Core API
TheMap object provides a standardized set of methods for mutation and retrieval. TypeScript ensures that arguments passed to these methods adhere to the defined <K, V> generics.
-
set(key: K, value: V): thisAdds or updates an element with a specified key and value. Returns theMapinstance, allowing for method chaining.
-
get(key: K): V | undefinedReturns the value associated with the key. If the key does not exist, it returnsundefined. TypeScript automatically types the return value as a union of the value type andundefined.
-
has(key: K): booleanReturns a boolean asserting whether a value has been associated with the key in theMap.
-
delete(key: K): booleanRemoves the specified element from theMap. Returnstrueif the element existed and was removed, orfalseif the element did not exist.
-
clear(): voidRemoves all key-value pairs from theMap, resetting its size to zero.
-
size: numberA read-only property that returns the total number of elements currently in theMap.
Iteration Protocols
Map implements the iterable protocol, meaning it can be used directly with for...of loops. It yields key-value pairs as tuples ([K, V]).
Map provides methods that return IterableIterator objects for specific parts of the collection:
-
keys(): IterableIterator<K>Returns a new iterator object containing the keys for each element in insertion order. -
values(): IterableIterator<V>Returns a new iterator object containing the values for each element in insertion order. -
entries(): IterableIterator<[K, V]>Returns a new iterator object containing an array of[key, value]for each element in insertion order. This is the default iterator behavior of theMapobject.
Key Equality
Key equality in aMap is evaluated using the SameValueZero algorithm. This means NaN is considered equal to NaN (even though NaN !== NaN in standard JavaScript), and all other values are considered equal according to the semantics of the strict equality (===) operator.
When using objects or arrays as keys, equality is determined by memory reference, not structural shape.
Master TypeScript with Deep Grasping Methodology!Learn More





