Explicit resource management arrived in TypeScript 5.2, which is a try/finally that cannot be forgotten.
class TempDir implements Disposable {
[Symbol.dispose]() { fs.rmSync(this.path, { recursive: true }) }
}
{
using dir = new TempDir()
writeFixtures(dir.path)
runTest(dir.path)
} // disposed here, including on a throw
// and the async form
await using conn = await pool.acquire()
The disposal happens at the end of the enclosing block rather than the function, which is the part that differs from a destructor and makes it precise. It needs a polyfill for Symbol.dispose on Node 18 and the downlevel emit is a real try/finally, so there is no runtime requirement beyond the symbol. Useful for temporary directories, locks and connections; not much else here yet.