sindresorhus/is · error · TypeError
Expected value which is `WeakRef`, received value of type `$
Error message
Expected value which is `WeakRef`, received value of type `${is(value)}`. What it means
Thrown by `assertWeakRef()` when the value is not a WeakRef instance. `isWeakRef` checks the internal toString tag (`getObjectType(value) === 'WeakRef'`), so only real `new WeakRef(target)` objects pass. The error message reports the actual detected type of the received value so the mismatch is immediately visible.
Source
Thrown at source/index.ts:1995
}
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 {
if (!isWhitespaceString(value)) {
throw new TypeError(message ?? typeErrorMessage('whitespace string', value));
}
}
export default is;
View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Pass the WeakRef wrapper, not its target: create it with `new WeakRef(target)` and don't call `.deref()` before the assertion.
- If you meant to validate the dereferenced target, assert the target's own type (e.g. `assertObject`) after checking `deref()` didn't return undefined.
- Verify the runtime actually supports native WeakRef (Node ≥14.6, no faux polyfill) — mocked WeakRef implementations fail the toString-tag check.
Example fix
// before const ref = new WeakRef(target); assertWeakRef(ref.deref()); // throws — deref() returns the target, not a WeakRef // after const ref = new WeakRef(target); assertWeakRef(ref); // passes
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof WeakRef !== 'undefined' && is.weakRef(value)) {
assert.weakRef(value); // safe
} Type guard
import is from '@sindresorhus/is';
function ensureWeakRef(value: unknown): WeakRef<object> | undefined {
return is.weakRef(value) ? value : undefined;
} Try / catch
try {
assert.weakRef(value);
} catch (error) {
if (error instanceof TypeError) {
handleInvalidInput(error.message);
} else {
throw error;
}
} Prevention
- Check is.weakRef(value) before asserting when input origin is untrusted
- WeakRef requires ES2021+ runtimes (Node 14.6+); verify typeof WeakRef !== 'undefined' before relying on it in older environments
- Do not confuse the WeakRef wrapper with its referent — pass the WeakRef itself, not weakRef.deref()
- Construct with new WeakRef(target) where target is an object; WeakRefs cannot wrap primitives
When it happens
Trigger: Calling `assertWeakRef(value)` (or `is.assert.weakRef(value)`) with the referenced target object itself instead of a WeakRef wrapping it, with the result of `weakRef.deref()` (which returns the target or undefined, never a WeakRef), or with null/undefined. Also fails on any polyfill/mock whose Symbol.toStringTag is not 'WeakRef'.
Common situations: Mixing up a WeakRef and its dereferenced target is the classic mistake — passing `ref.deref()` where the ref itself is expected. Also hit in older runtimes: WeakRef requires Node.js 14.6+/modern browsers, and test environments or bundler shims that stub it produce objects failing the tag check.
Related errors
- Expected value which is `WeakMap`, 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/5511ef85caa57763.json.
Report an issue: GitHub ↗.