In the upcoming TypeScript 5.2, it’s planed to add a very interesting and useful feature called using. Similar functionality already exists in other languages: in C# it’s using, and in Go it’s defer. The main idea is that using allows you to automatically release a resource (by calling a special method for this) after it exits the scope of the block. For example, if we have a method getResource that has implemented the method [Symbol.dispose](), then we can write the following code and the Symbol.dispose method will be executed automatically at the end of the main function, as if this construct were wrapped in a try finally block.

function getResource() {
    return {
        execute: () => console.log('execute'),
        [Symbol.dispose]: () => console.log('dispose'),
    };
}

/*
function main() {
    const resource = getResource();
    try {
        resource.execute()
    } finally {
        resource[Symbol.dispose]();
    }
}
*/
function main() {
    const resource = using getResource();
    resource.execute();
}
main();

Such behavior can be particularly useful in Node.js for working with databases, for example, but I am sure that similar syntactic sugar can be useful in many other places. To see a more detailed discussion and implementation of the functionality, you can check out the pull request https://github.com/microsoft/TypeScript/pull/54505 for now, and TypeScript 5.2 is expected to be released on August 22.