TypeScript · Senior · Question 28 of 75

What are using declarations (TS 5.2) for?

{
  using file = openFile('log.txt');
  // ...
} // file[Symbol.dispose]() is called automatically

Explanation

It implements the ECMAScript Explicit Resource Management proposal: the resource is released even if an exception is thrown - like try/finally, but shorter. For async resources there's await using and Symbol.asyncDispose. Handy for connections, files, locks and subscriptions in tests.

Further reading

Problem with a question

TypeScript · Senior · question 28

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz