sindresorhus/is · error · TypeError
Expected value which is `Map`, received value of type `${is(
Error message
Expected value which is `Map`, received value of type `${is(value)}`. What it means
Thrown by `assertMap()` (source/index.ts:1705) when `isMap()` fails; it requires the object type tag to be exactly 'Map'. Plain objects, WeakMaps, and Map-serialized-to-JSON values all fail, since the library refuses to treat lookalike structures as real Maps.
Source
Thrown at source/index.ts:1707
throw new TypeError(message ?? typeErrorMessage('Int8Array', value));
}
}
export function assertInteger(value: unknown, message?: string): asserts value is number {
if (!isInteger(value)) {
throw new TypeError(message ?? typeErrorMessage('integer', value));
}
}
export function assertIterable<T = unknown>(value: unknown, message?: string): asserts value is Iterable<T> {
if (!isIterable(value)) {
throw new TypeError(message ?? typeErrorMessage('Iterable', value));
}
}
export function assertMap<Key = unknown, Value = unknown>(value: unknown, message?: string): asserts value is Map<Key, Value> {
if (!isMap(value)) {
throw new TypeError(message ?? typeErrorMessage('Map', value));
}
}
export function assertNan(value: unknown, message?: string): asserts value is number {
if (!isNan(value)) {
throw new TypeError(message ?? typeErrorMessage('NaN', value));
}
}
export function assertNativePromise<T = unknown>(value: unknown, message?: string): asserts value is Promise<T> {
if (!isNativePromise(value)) {
throw new TypeError(message ?? typeErrorMessage('native Promise', value));
}
}
export function assertNegativeInteger(value: unknown, message?: string): asserts value is number {
if (!isNegativeInteger(value)) {
throw new TypeError(message ?? typeErrorMessage('negative integer', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Reconstruct after deserialization: `new Map(Object.entries(parsedObject))` or `new Map(pairsArray)`.
- Serialize Maps explicitly (`[...map.entries()]`) so the receiving side can rebuild them.
- If a plain-object dictionary is acceptable for the use case, assert `assertPlainObject()` instead and access via key lookup.
Example fix
// before const cache = JSON.parse(raw); assertMap(cache); // throws — JSON has no Map // after const cache = new Map(Object.entries(JSON.parse(raw))); assertMap(cache);
Defensive patterns
Strategy: type-guard
Validate before calling
if (!(value instanceof Map)) {
value = new Map(Object.entries(value ?? {})); // if a plain object is acceptable input
} Type guard
const isMap = (v: unknown): v is Map<unknown, unknown> => v instanceof Map;
Prevention
- Plain objects and JSON-parsed data are never Maps — convert with new Map(Object.entries(obj)) at the boundary
- JSON.parse cannot produce a Map; deserialize explicitly if your wire format encodes map data
- WeakMap does not pass an is.map check — they are distinct types
When it happens
Trigger: Calling `assertMap()` with a plain object used as a dictionary, a WeakMap (tag 'WeakMap'), an array of [key, value] pairs, or the result of `JSON.parse` on serialized Map data (Maps serialize to `{}`).
Common situations: The JSON round-trip is the top cause: `JSON.stringify(new Map())` yields '{}', so anything persisted/transported loses Map-ness. Also codebases mixing object-dictionaries with Maps, or receiving `Object.fromEntries` output where a Map is expected.
Related errors
- Expected value which is `bigint`, received value of type `${
- Expected value which is `Date`, received value of type `${is
- Expected value which is `empty map`, received value of type
- Expected value which is `Set`, received value of type `${is(
- Expected value which is `Array`, received value of type `${i
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/2b865b5f49678e92.json.
Report an issue: GitHub ↗.