Garbage Collection
Garbage collection is the JavaScript engine's job: unreachable objects are reclaimed. TypeScript types do not keep objects alive and do not free them. A type can only hide a reference you still hold in the JavaScript.
Detailed Explanation
V8, JavaScriptCore, and SpiderMonkey collect objects that the running program cannot reach. TypeScript is not in that loop. After emit, there are no types to scan. User, WeakMap, and readonly do not change reachability.
What does change reachability is the JavaScript you wrote, which TypeScript typed:
Closures. A method you pass as a callback closes over this or over a large object. The type of the callback is (ev: Event) => void. The type does not mention the closed-over cache. The cache stays alive until the listener is removed. The leak is the same as in JavaScript; the type just will not point at the captured binding.
Detached DOM. A type says HTMLElement. You removed the node from the tree but kept it in an array typed HTMLElement[]. The array is a real reference. GC cannot take the node. lib.dom will not warn.
Timers and workers. A setInterval handle typed as number or ReturnType<typeof setInterval> still holds the callback, which still holds its closure. The type of the handle is not a WeakRef.
WeakMap / WeakSet / WeakRef are the typed APIs for references that do not keep the key (or target) alive. See weak collections. Their types enforce that keys are objects (or symbols). They do not promise when collection happens.
Generics and aliases cannot leak. type Huge = Record<string, User> is erased. A Map<string, User> value is not erased and will hold every User you set until you delete or drop the map.
When you debug a leak in a TypeScript app, take a heap snapshot of the running JavaScript. Look for detached nodes, listeners, and caches. Then find the TypeScript binding that still points at them. Changing a type from User to Readonly<User> will not shrink the heap.
For mark-and-sweep, generations, and how closures retain memory, see the JavaScript garbage-collection pages. This page exists so nobody treats a type as a lifetime annotation. TypeScript has no lifetime annotation.
Code Examples
1. A typed listener still retains its closure.
// 1. A typed listener still retains its closure.
type Cache = Map<string, string[]>;
function attach(el: HTMLElement, cache: Cache): () => void {
const onClick = (): void => {
cache.set("last", ["click"]);
};
el.addEventListener("click", onClick);
return () => el.removeEventListener("click", onClick);
}2. Dropping the type does not drop the reference; dropping the variable does.
// 2. Dropping the type does not drop the reference; dropping the variable does.
let users: { id: string }[] | undefined = [{ id: "u1" }];
users = undefined; // the array can now be collected3. WeakMap types a non-retaining cache.
// 3. WeakMap types a non-retaining cache.
const notes = new WeakMap<Element, string>();
function remember(el: Element, note: string): void {
notes.set(el, note);
}Common Gotchas
`readonly` and brands do not affect GC
only references in values do.
A type-only import cannot retain a value
if something is alive, a runtime import or a binding holds it.
Heap snapshots show JS, not `.ts` types
source maps help you jump back to the TypeScript that allocated the object.
`WeakRef` is not a type
`WeakRef<T>` is a runtime object. `T` alone is not weak.