{"id":"4e26a4d727cbf6b6","repo":"sindresorhus/is","slug":"expected-value-which-is-weakset-received-value","errorCode":null,"errorMessage":"Expected value which is `WeakSet`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `WeakSet`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":2002,"sourceCode":"\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {\n\tif (!isWeakMap(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakMap', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakRef<T extends object = object>(value: unknown, message?: string): asserts value is WeakRef<T> {\n\tif (!isWeakRef(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakRef', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakSet<T extends object = object>(value: unknown, message?: string): asserts value is WeakSet<T> {\n\tif (!isWeakSet(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakSet', value));\n\t}\n}\n\nexport function assertWhitespaceString(value: unknown, message?: string): asserts value is string {\n\tif (!isWhitespaceString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('whitespace string', value));\n\t}\n}\n\nexport default is;\n\nexport type {\n\tArrayLike,\n\tClass,\n\tEvenInteger,\n\tFiniteNumber,\n\tInteger,\n\tNaN,","sourceCodeStart":1984,"sourceCodeEnd":2020,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1984-L2020","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst visited = new Set();\nassertWeakSet(visited); // throws TypeError\n\n// after\nconst visited = new WeakSet();\nassertWeakSet(visited); // passes","handlingStrategy":"type-guard","validationCode":"if (is.weakSet(value)) {\n  assert.weakSet(value); // safe\n}","typeGuard":"import is from '@sindresorhus/is';\n\nfunction ensureWeakSet(value: unknown): WeakSet<object> | undefined {\n  return is.weakSet(value) ? value : undefined;\n}","tryCatchPattern":"try {\n  assert.weakSet(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    handleInvalidInput(error.message);\n  } else {\n    throw error;\n  }\n}","preventionTips":["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"],"tags":["typescript","type-assertion","weakset","runtime-validation","sindresorhus-is"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}