{"id":"5511ef85caa57763","repo":"sindresorhus/is","slug":"expected-value-which-is-weakref-received-value","errorCode":null,"errorMessage":"Expected value which is `WeakRef`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `WeakRef`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1995,"sourceCode":"}\n\nexport function assertValidLength(value: unknown, message?: string): asserts value is number {\n\tif (!isValidLength(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid length', value));\n\t}\n}\n\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","sourceCodeStart":1977,"sourceCodeEnd":2013,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1977-L2013","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","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."],"exampleFix":"// before\nconst ref = new WeakRef(target);\nassertWeakRef(ref.deref()); // throws — deref() returns the target, not a WeakRef\n\n// after\nconst ref = new WeakRef(target);\nassertWeakRef(ref); // passes","handlingStrategy":"type-guard","validationCode":"if (typeof WeakRef !== 'undefined' && is.weakRef(value)) {\n  assert.weakRef(value); // safe\n}","typeGuard":"import is from '@sindresorhus/is';\n\nfunction ensureWeakRef(value: unknown): WeakRef<object> | undefined {\n  return is.weakRef(value) ? value : undefined;\n}","tryCatchPattern":"try {\n  assert.weakRef(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    handleInvalidInput(error.message);\n  } else {\n    throw error;\n  }\n}","preventionTips":["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"],"tags":["typescript","type-assertion","weakref","garbage-collection","runtime-validation","sindresorhus-is"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}