TypeScript Syntax

using — synchronous resource disposal

Registers a disposable value for cleanup when control exits its lexical scope, including abrupt exit. Values must supply a compatible Symbol.dispose method or be nullish. Multiple resources are disposed in reverse acquisition order. The fixture uses a local mock resource; disposal requires suitable library declarations and runtime symbols. Observed output: 'body\nsecond\nfirst'. Local verification: TypeScript 5.8.3, strict mode, ES2022, CommonJS, Node.js 22.16.0. Disposable library declarations are enabled for the fixture suite.

Formal Syntax & Structure

typescript syntaxtypescript
using resource = disposableExpression;

Working Example

Example Snippettypescript
function run() {  using first = { [Symbol.dispose]() { console.log("first"); } };  using second = { [Symbol.dispose]() { console.log("second"); } };  console.log("body");}run();
TypeScript Reference