Skip to content

isWeakMapKey

Checks whether a value can be used as a key in a WeakMap (or a WeakSet member / WeakRef target) — an object, a function (despite typeof reporting 'function', functions are objects), or an unregistered symbol (a plain Symbol(...) or a well-known symbol like Symbol.iterator).

Symbols registered via Symbol.for(...) are excluded: since they live forever in the global symbol registry, the language disallows them as weak references (ES2023’s “Symbols as WeakMap keys”). This is the part of the check easy to get wrong — typeof value === 'symbol' alone accepts registered symbols too, which actually throw at WeakMap.prototype.set.

Available since v3.0.8

import { isWeakMapKey } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isWeakMapKey } from 'helpers4/guard';
isWeakMapKey(value: unknown): value is symbol | object
ParameterTypeDescription
valueunknownThe value to check

value is symbol | object — true if value is a valid WeakMap/WeakSet key or WeakRef target

Objects and functions are valid WeakMap keys

Section titled “Objects and functions are valid WeakMap keys”

Functions pass despite typeof reporting “function” — they are objects.

isWeakMapKey({})       // => true
isWeakMapKey(() => {}) // => true
isWeakMapKey(42)       // => false

Unregistered symbols pass, registered ones do not

Section titled “Unregistered symbols pass, registered ones do not”

Symbol.for(…) symbols live forever in the global registry, so the language disallows them as weak references.

isWeakMapKey(Symbol('x'))     // => true
isWeakMapKey(Symbol.for('x')) // => false

View source on GitHub