sindresorhus/is · error · TypeError
Expected value which is `WeakMap`, received value of type `$
Error message
Expected value which is `WeakMap`, received value of type `${is(value)}`. What it means
Thrown by `assertWeakMap()` in the @sindresorhus/is library when the given value is not a WeakMap instance. The check (`isWeakMap`) compares the value's `Object.prototype.toString` tag via `getObjectType(value) === 'WeakMap'`, so only genuine WeakMap objects pass. Assert functions in this library throw a TypeError with a message that interpolates the detected type of the received value, letting callers fail fast instead of silently proceeding with the wrong type.
Source
Thrown at source/index.ts:1988
}
}
export function assertValidDate(value: unknown, message?: string): asserts value is Date {
if (!isValidDate(value)) {
throw new TypeError(message ?? typeErrorMessage('valid Date', value));
}
}
export function assertValidLength(value: unknown, message?: string): asserts value is number {
if (!isValidLength(value)) {
throw new TypeError(message ?? typeErrorMessage('valid length', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {
if (!isWeakMap(value)) {
throw new TypeError(message ?? typeErrorMessage('WeakMap', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakRef<T extends object = object>(value: unknown, message?: string): asserts value is WeakRef<T> {
if (!isWeakRef(value)) {
throw new TypeError(message ?? typeErrorMessage('WeakRef', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakSet<T extends object = object>(value: unknown, message?: string): asserts value is WeakSet<T> {
if (!isWeakSet(value)) {
throw new TypeError(message ?? typeErrorMessage('WeakSet', value));
}
}
export function assertWhitespaceString(value: unknown, message?: string): asserts value is string {View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Pass an actual `new WeakMap()` instance — check the call site to see whether a Map, plain object, or undefined is being passed instead.
- If the value can legitimately be a Map or WeakMap, branch with `is.weakMap(value)` / `is.map(value)` instead of asserting one type.
- If keys may be primitives, use a `Map` and change the assertion to `assertMap` — WeakMap keys must be objects, which is often why a Map was substituted.
- Guard optional parameters before asserting: `if (cache !== undefined) assertWeakMap(cache)`.
Example fix
// before const cache = new Map(); assertWeakMap(cache); // throws TypeError // after const cache = new WeakMap(); assertWeakMap(cache); // passes
Defensive patterns
Strategy: type-guard
Validate before calling
if (is.weakMap(value)) {
assert.weakMap(value); // safe, will not throw
} Type guard
import is from '@sindresorhus/is';
function ensureWeakMap(value: unknown): WeakMap<object, unknown> | undefined {
return is.weakMap(value) ? value : undefined;
} Try / catch
try {
assert.weakMap(value);
} catch (error) {
if (error instanceof TypeError) {
// value was not a WeakMap; error.message names the actual type
handleInvalidInput(error.message);
} else {
throw error;
}
} Prevention
- Call the boolean guard is.weakMap(value) before assert.weakMap when the input may legitimately not be a WeakMap; reserve assert for invariants that should never fail
- Remember a plain Map is not a WeakMap — construct with new WeakMap(), not new Map()
- WeakMaps do not survive structured clone or JSON serialization, so values coming from postMessage/JSON parsing will never be WeakMaps
- Type function parameters as WeakMap<K, V> in TypeScript so misuse is caught at compile time instead of runtime
When it happens
Trigger: Calling `assertWeakMap(value)` (or `is.assert.weakMap(value)`) with anything whose toString tag is not 'WeakMap' — e.g. a plain Map, a plain object used as a cache, null/undefined, or an object from another library that only mimics the WeakMap API. Cross-realm values pass since the check is tag-based, but a Proxy or a subclass with an overridden Symbol.toStringTag will fail.
Common situations: Developers commonly hit this when refactoring a `Map` cache to `WeakMap` (or vice versa) and one call site keeps passing the old collection type; when a memoization/cache argument is optional and `undefined` reaches the assertion; or when deserialized data (JSON has no WeakMap representation) is passed where a live WeakMap is expected.
Related errors
- Expected value which is `WeakRef`, received value of type `$
- Expected value which is `WeakSet`, received value of type `$
- Expected value which is `whitespace string`, received value
- Expected value which is `empty map`, received value of type
- Expected value which is `empty object`, received value of ty
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/9734fd0105aeda16.json.
Report an issue: GitHub ↗.