sindresorhus/is · error · TypeError
Expected value which is `WeakSet`, received value of type `$
Error message
Expected value which is `WeakSet`, received value of type `${is(value)}`. What it means
Thrown by `assertWeakSet()` when the value is not a WeakSet instance. `isWeakSet` compares the toString tag (`getObjectType(value) === 'WeakSet'`), so a regular Set, an Array, or an API-compatible mock will all be rejected. Like the other assert functions in this library, it throws a TypeError naming the expected type and the detected type of what was actually received.
Source
Thrown at source/index.ts:2002
// 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;
export type {
ArrayLike,
Class,
EvenInteger,
FiniteNumber,
Integer,
NaN,View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Construct the value with `new WeakSet()` instead of `new Set()` at the failing call site.
- If members can be primitives, keep a `Set` and assert with `assertSet` — WeakSet cannot hold primitives.
- If both collection types are valid inputs, use non-throwing guards (`is.weakSet(value) || is.set(value)`) and branch instead of asserting.
Example fix
// before const visited = new Set(); assertWeakSet(visited); // throws TypeError // after const visited = new WeakSet(); assertWeakSet(visited); // passes
Defensive patterns
Strategy: type-guard
Validate before calling
if (is.weakSet(value)) {
assert.weakSet(value); // safe
} Type guard
import is from '@sindresorhus/is';
function ensureWeakSet(value: unknown): WeakSet<object> | undefined {
return is.weakSet(value) ? value : undefined;
} Try / catch
try {
assert.weakSet(value);
} catch (error) {
if (error instanceof TypeError) {
handleInvalidInput(error.message);
} else {
throw error;
}
} Prevention
- Use the boolean guard is.weakSet(value) as a branch condition rather than catching the assert's TypeError for expected non-WeakSet inputs
- A Set is not a WeakSet — make sure the producing code calls new WeakSet(), not new Set()
- WeakSets can only hold objects and are not serializable; anything deserialized from JSON or IPC will fail this assert
- Annotate parameters as WeakSet<object> in TypeScript so the compiler flags wrong collection types before runtime
When it happens
Trigger: Calling `assertWeakSet(value)` (or `is.assert.weakSet(value)`) with a `Set`, an array of objects, null/undefined, or a duck-typed object exposing add/has/delete. Any value whose Symbol.toStringTag-derived type is not exactly 'WeakSet' triggers it.
Common situations: Most often hit when converting a `Set` used for object-membership tracking (e.g. visited-node sets in graph/serialization code) to a `WeakSet` and one code path still constructs a Set; or when trying to use a WeakSet with primitive members — since WeakSet only holds objects, developers fall back to Set and the assertion then fails.
Related errors
- Expected value which is `WeakMap`, received value of type `$
- Expected value which is `WeakRef`, 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/4e26a4d727cbf6b6.json.
Report an issue: GitHub ↗.