Question presented to candidate:
"TypeScript 5.2 added using and await using. What do they do, how does an object opt in with Symbol.dispose, in what order does cleanup run, and what do you need in your config and runtime to use them today?"
What a strong answer should cover:
using x = exprdeclares a const-like, block-scoped binding whose[Symbol.dispose]()is called when the block exits, at the end of the block, onreturnand onthrow; several bindings dispose in reverse order (last in, first out)- The types:
Disposable([Symbol.dispose](): void) andAsyncDisposable([Symbol.asyncDispose](): PromiseLike<void>); the initializer must be one of those ornull/undefined(TS2850), andawait usingonly works in async code (TS2852) await usingawaitsSymbol.asyncDispose(falling back toSymbol.dispose) before leaving the block- Errors: a body error is rethrown after cleanup; if cleanup also throws, both are wrapped in a
SuppressedError(error= the newer,suppressed= the earlier) - Setup:
libmust includeesnextoresnext.disposable; for older targets TypeScript emits helper code (__addDisposableResource,__disposeResources); runtimes without the built-ins need at leastSymbol.dispose/Symbol.asyncDisposepolyfills
Code / implementation expected: Yes β a small resource class with [Symbol.dispose], two using bindings, and the order of the log lines.