Skip to main content

isMap()

isMap(value): value is Map<unknown, unknown>

Checks if a value is a Map instance.


Parametersโ€‹

value: unknownโ€‹

The value to check.


Returns: value is Map<unknown, unknown>โ€‹

true if the value is a Map, false otherwise.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Exampleโ€‹

isMap(new Map());              // => true
isMap(new Map([['a', 1]])); // => true
isMap({}); // => false
isMap(new WeakMap()); // => false

How it works?โ€‹

Type guard that checks if a value is a Map.

Type Narrowingโ€‹

Common Checksโ€‹

ValueResult
new Map()checkmark true
new Map([['a', 1]])checkmark true
new WeakMap()cross false
{}cross false
Object.fromEntries([])cross false

Use Casesโ€‹

Identify key-value collections ๐Ÿ“Œโ€‹

Check if a value is a standard Map object. Perfect for distinguishing Maps from plain objects or other iterables.

if (isMap(cache)) {
cache.set(key, value);
}