# Disable WeakRef and FinalizationRegistry (`ts-compat/no-weakrefs`) 💼 This rule is enabled in the ✅ `recommended` config. `WeakRef` and `FinalizationRegistry` are ES2021 features. They have never been supported by any version of IE or legacy Edge, and they need Chrome 84, Edge 84, Firefox 79, Safari 14.1, Opera 70 or iOS Safari 14.5 to run. Neither of them can be polyfilled. Whether the target is still alive is answered by the garbage collector, and a shim which keeps a strong reference would break the very semantics the caller asked for — the object is never collected and the leak is invisible. `FinalizationRegistry` is worse: the callback runs at a time the code can not control, so a shim which fires it eagerly would report the target as dead while it is still reachable. Use `WeakMap`, which is keyed by the object itself, or clear the reference explicitly when the owner is released. ## Rule Details Examples of **incorrect** code for this rule: ```ts const ref = new WeakRef({ a: 1 }); const target = ref.deref(); if(target) { target.a; } WeakRef.prototype.deref; const registry = new FinalizationRegistry(value => { console.log(value); }); registry.register({ a: 1 }, "held"); registry.unregister("held"); FinalizationRegistry.prototype.register; ``` Examples of **correct** code for this rule: ```ts const wm = new WeakMap(); const key = {}; wm.set(key, 1); const value = wm.get(key); const holders = new Set(); holders.add(key); holders.delete(key); ``` ## When Not To Use It * Every target browser supports the weak reference APIs. * The code runs in an architecture layer, such as a cache, and the unsupported browsers fall back to a strong reference there. ## Further Reading * [MDN WeakRef](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) * [MDN FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry)