{"id":"9734fd0105aeda16","repo":"sindresorhus/is","slug":"expected-value-which-is-weakmap-received-value","errorCode":null,"errorMessage":"Expected value which is `WeakMap`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `WeakMap`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1988,"sourceCode":"\t}\n}\n\nexport function assertValidDate(value: unknown, message?: string): asserts value is Date {\n\tif (!isValidDate(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid Date', value));\n\t}\n}\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 {","sourceCodeStart":1970,"sourceCodeEnd":2006,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1970-L2006","documentation":"Thrown by `assertWeakMap()` in the @sindresorhus/is library when the given value is not a WeakMap instance. The check (`isWeakMap`) compares the value's `Object.prototype.toString` tag via `getObjectType(value) === 'WeakMap'`, so only genuine WeakMap objects pass. Assert functions in this library throw a TypeError with a message that interpolates the detected type of the received value, letting callers fail fast instead of silently proceeding with the wrong type.","triggerScenarios":"Calling `assertWeakMap(value)` (or `is.assert.weakMap(value)`) with anything whose toString tag is not 'WeakMap' — e.g. a plain Map, a plain object used as a cache, null/undefined, or an object from another library that only mimics the WeakMap API. Cross-realm values pass since the check is tag-based, but a Proxy or a subclass with an overridden Symbol.toStringTag will fail.","commonSituations":"Developers commonly hit this when refactoring a `Map` cache to `WeakMap` (or vice versa) and one call site keeps passing the old collection type; when a memoization/cache argument is optional and `undefined` reaches the assertion; or when deserialized data (JSON has no WeakMap representation) is passed where a live WeakMap is expected.","solutions":["Pass an actual `new WeakMap()` instance — check the call site to see whether a Map, plain object, or undefined is being passed instead.","If the value can legitimately be a Map or WeakMap, branch with `is.weakMap(value)` / `is.map(value)` instead of asserting one type.","If keys may be primitives, use a `Map` and change the assertion to `assertMap` — WeakMap keys must be objects, which is often why a Map was substituted.","Guard optional parameters before asserting: `if (cache !== undefined) assertWeakMap(cache)`."],"exampleFix":"// before\nconst cache = new Map();\nassertWeakMap(cache); // throws TypeError\n\n// after\nconst cache = new WeakMap();\nassertWeakMap(cache); // passes","handlingStrategy":"type-guard","validationCode":"if (is.weakMap(value)) {\n  assert.weakMap(value); // safe, will not throw\n}","typeGuard":"import is from '@sindresorhus/is';\n\nfunction ensureWeakMap(value: unknown): WeakMap<object, unknown> | undefined {\n  return is.weakMap(value) ? value : undefined;\n}","tryCatchPattern":"try {\n  assert.weakMap(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // value was not a WeakMap; error.message names the actual type\n    handleInvalidInput(error.message);\n  } else {\n    throw error;\n  }\n}","preventionTips":["Call the boolean guard is.weakMap(value) before assert.weakMap when the input may legitimately not be a WeakMap; reserve assert for invariants that should never fail","Remember a plain Map is not a WeakMap — construct with new WeakMap(), not new Map()","WeakMaps do not survive structured clone or JSON serialization, so values coming from postMessage/JSON parsing will never be WeakMaps","Type function parameters as WeakMap<K, V> in TypeScript so misuse is caught at compile time instead of runtime"],"tags":["typescript","type-assertion","weakmap","runtime-validation","sindresorhus-is"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}