Weak Collections
WeakMap, WeakSet, and WeakRef hold references that do not keep the target object alive. TypeScript types them as generics whose keys or targets must be objects (or symbols, for WeakMap).
Detailed Explanation
Ordinary Map and Set keep their entries alive. If you key a cache by a DOM node or a class instance, that cache is a leak until you delete the entry. Weak collections opt out of that: when the rest of the program drops the object, the engine may collect it and the weak entry disappears.
WeakMap<K extends WeakKey, V> maps a weak key to a value. The value is still strong. If V points back at K, you have a leak again. WeakSet<T extends WeakKey> is a set of weak object (or symbol) membership. WeakRef<T extends object> is a single weak pointer; .deref() returns T | undefined. FinalizationRegistry<T> runs a callback after a registered object is collected; T is the held value you passed in, which must not be the collected object if you want the callback to see it.
WeakKey in current lib types is object | symbol. Primitives cannot be weak keys. WeakMap<string, User> is a type error. That is the type system encoding the runtime rule.
These collections do not iterate. There is no keys(), no size you can trust for application logic, no typed entry tuple. That absence is in the type: you will not find those methods on WeakMap. If you need to iterate, you want Map and a manual cleanup, or a two-level structure.
TypeScript does not model "this object has been collected." After deref(), you handle undefined. There is no branded Alive<T>. Tests that depend on collection happening in a given turn are already flaky in JavaScript; types will not stabilize them.
Use WeakMap for per-object metadata you do not want to put on the object (and do not want to leak): private notes on a DOM node, a memo table keyed by a request object, the implementation of a #-like store on old targets. Use WeakRef rarely, when you have a cache of large objects you can recompute. Use FinalizationRegistry even more rarely; it is not a destructor and it is not typed as one.
See JavaScript WeakMap / WeakRef for the GC semantics. The TypeScript extra is the WeakKey constraint and deref(): T | undefined.
Code Examples
1. Per-element metadata that does not keep the element alive.
// 1. Per-element metadata that does not keep the element alive.
const labels = new WeakMap<Element, string>();
function setLabel(el: Element, label: string): void {
labels.set(el, label);
}
function getLabel(el: Element): string | undefined {
return labels.get(el);
}2. WeakRef.deref is T | undefined.
// 2. WeakRef.deref is T | undefined.
type User = { id: string };
const cache = new Map<string, WeakRef<User>>();
function remember(user: User): void {
cache.set(user.id, new WeakRef(user));
}
function lookup(id: string): User | undefined {
return cache.get(id)?.deref();
}3. WeakSet membership.
// 3. WeakSet membership.
const seen = new WeakSet<object>();
function once(obj: object, run: () => void): void {
if (seen.has(obj)) return;
seen.add(obj);
run();
}Common Gotchas
`WeakMap<string, V>` is illegal
keys must be `WeakKey`. Use `Map<string, V>` for string keys.
A strong `V` that points at `K` keeps `K` alive
do not store the key inside the value.
No iteration, no `size` for logic
if you need to list entries, a weak collection is the wrong type.
`deref()` racing with collection is expected
handle `undefined` every time; do not assert.