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

  1. Pass the WeakRef wrapper, not its target: create it with `new WeakRef(target)` and don't call `.deref()` before the assertion.
  2. If you meant to validate the dereferenced target, assert the target's own type (e.g. `assertObject`) after checking `deref()` didn't return undefined.
  3. 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

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


AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31). Data as JSON: /data/errors/5511ef85caa57763.json. Report an issue: GitHub ↗.