Sub topic 14
Using the Feature Today: Runtimes, TypeScript, and Babel
This chapter assumes you already know the using and await using syntax, the disposable protocol, and DisposableStack from earlier chapters. You should also be comfortable editing a tsconfig.json or a Babel config file. The question here is practical: how do you ship code that uses explicit resource management when your users run different engines?
Two Separate Pieces: Syntax and Runtime
Explicit resource management has two parts, and tools handle each part differently.
| Part | What it includes | Who provides it |
|---|---|---|
| Syntax | `using`, `await using`, `for (using x of ...)` | The engine's parser, or a compiler that rewrites it |
| Runtime | `Symbol.dispose`, `Symbol.asyncDispose`, `DisposableStack`, `AsyncDisposableStack`, `SuppressedError` | The engine, or a polyfill |
A compiler can rewrite using into try/finally-style helper code. A compiler cannot create DisposableStack for you. If the target engine lacks the runtime globals, you need a polyfill such as core-js. Many build failures come from fixing one part and forgetting the other.
Native Runtime Support
Recent V8-based environments ship the feature natively: Chromium-based browsers from version 134 and Node.js from version 24. Firefox added support around version 134. Support in other engines, including Safari's JavaScriptCore, has lagged, and runtimes like Deno and Bun have changed their support over time. These details move quickly, so check a compatibility table such as MDN or caniuse for your actual targets. Don't rely on version numbers from memory.
You can feature-detect the runtime part directly. Syntax is harder. A parser that doesn't understand using rejects the whole file before any of your code runs, so the check has to parse a separate string:
function supportsUsingSyntax() {
try {
new Function("{ using x = null; }");
return true;
} catch {
return false;
}
}
const runtimeReady =
typeof Symbol.dispose === "symbol" &&
typeof Symbol.asyncDispose === "symbol" &&
typeof DisposableStack === "function" &&
typeof SuppressedError === "function";
console.log("syntax:", supportsUsingSyntax());
console.log("runtime:", runtimeReady);TypeScript
TypeScript has supported using and await using since version 5.2. You need two settings:
- A
libthat includes the type declarations forDisposable,AsyncDisposable,DisposableStack, and the dispose symbols. - A
target. If the target is older than the engines you run on, TypeScript lowersusinginto helper calls (__addDisposableResourceand__disposeResources).
{
"compilerOptions": {
"target": "es2022",
"lib": ["es2022", "esnext.disposable", "dom"],
"module": "nodenext",
"importHelpers": true
}
}The helper code that TypeScript emits looks up Symbol.dispose at runtime. If that symbol is missing, the code throws. When you can't add a full polyfill, a minimal shim at your entry point is enough for plain using:
Symbol.dispose ??= Symbol("Symbol.dispose");
Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");This shim does not provide DisposableStack or SuppressedError. Some engines lock down Symbol's properties, so prefer a real polyfill when you can.
Babel
Babel handles the syntax through @babel/plugin-proposal-explicit-resource-management:
{
"presets": [["@babel/preset-env", { "targets": "defaults" }]],
"plugins": ["@babel/plugin-proposal-explicit-resource-management"]
}For the runtime globals, load core-js once at your entry point:
import "core-js/proposals/explicit-resource-management";This import defines the dispose symbols, DisposableStack, AsyncDisposableStack, and SuppressedError where they are missing.
Common Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `SyntaxError: Unexpected identifier 'using'` | The engine doesn't parse `using` and nothing compiled it | Lower the TypeScript `target` or add the Babel plugin |
| `Cannot find name 'DisposableStack'` in TypeScript | `lib` is missing `esnext.disposable` | Add it to `lib` |
| `TypeError: Symbol.dispose is not defined` or `Object not disposable` at runtime | Compiled code ran without the dispose symbols | Add core-js or the symbol shim |
| `DisposableStack is not defined` | The syntax was compiled, but no polyfill was loaded | Import the core-js proposal entry |
Exercise
Your TypeScript project targets es2020 and compiles without errors, but in an older browser using stack = new DisposableStack() throws "DisposableStack is not defined". What is wrong, and how do you fix it?
Show answer
TypeScript lowered the using syntax and type-checked against the esnext.disposable lib, but types don't create runtime values. The browser has no DisposableStack global. Load a runtime polyfill, for example import "core-js/proposals/explicit-resource-management";, before any code that uses it.
Exercise
Why can't you detect using support with a plain if statement that contains a using declaration in the same file?
Show answer
The parser reads the whole file before running any of it. An engine that doesn't understand using throws a SyntaxError during parsing, so the if never runs. The check has to parse a separate string, for example with new Function, or the decision has to happen at build time.