What is the using keyword in TypeScript 5.2?

Answer

TypeScript 5.2 introduced the using and await using declarations, implementing the ECMAScript explicit resource management proposal. They automatically call a resource's [Symbol.dispose]() (or [Symbol.asyncDispose]()) method when the variable goes out of scope — similar to using in C# or try-with-resources in Java. Example: { using file = openFile("data.txt"); file.read(); } // file[Symbol.dispose]() is called automatically here. For async resources: await using conn = await getDbConnection(); // conn[Symbol.asyncDispose]() called on exit. No finally block needed. The DisposableStack and AsyncDisposableStack classes help manage multiple resources. This replaces the verbose try-finally pattern for resource cleanup. Useful for: file handles, database connections, network sockets, locks, and any resource that needs deterministic cleanup. TypeScript provides the Disposable and AsyncDisposable interfaces to type-check objects used with using.