On August 24, 2023, Microsoft shipped TypeScript 5.2 with a feature that looks small but, once tried, is hard to give up: the using keyword. It anticipates the ECMAScript Explicit Resource Management proposal (currently stage 3) and removes a chronic source of bugs in Node projects.
The problem: who closes the resource?
We have all written this:
const file = await fs.open("data.txt");
try {
// use the file
} finally {
await file.close();
}
It works, but it is verbose. When resources reach two, three or four, the nested try/finally blocks turn into a maze. And if someone forgets a finally, the resource stays open until the GC kicks in — or never.
The fix: using
With TypeScript 5.2 you write:
using file = await fs.open("data.txt");
// use the file
// on scope exit, file[Symbol.dispose]() is called
The runtime calls Symbol.dispose automatically when the scope ends, whether by normal return, exception or break. For async resources there is await using, which calls Symbol.asyncDispose.
When we actually need it
In the client onboarding scripts we write regularly we juggle database connections, file locks and external service sessions in the same flow. With using the code becomes linear and impossible to forget. We have also started wrapping Prisma transactions so they dispose automatically inside Server Actions.
function transaction() {
const tx = prisma.$begin();
return {
tx,
[Symbol.asyncDispose]: () => tx.commit(),
};
}
await using t = transaction();
await t.tx.user.update(...);
// auto commit
What you need to use it
The compile target must support Symbol.dispose. TypeScript polyfills older targets via the __addDisposableResource helper. On Node.js you need at least 20.x with a flag or, even better, the 22 LTS that landed in 2024. Newer runtimes (Bun, Deno) support it natively.
Is it worth migrating?
This is not the kind of feature that justifies a mass refactor. It is, however, one of those that quietly cuts bugs once the team picks it up. Our rule: using is mandatory for anything with open/close, connect/disconnect, lock/unlock. For everything else, leave it alone.