TypeScript Reference ยท Utility Types
TypeScript Disposable and AsyncDisposable types
Reference for the Disposable and AsyncDisposable interfaces in TypeScript 5.2+: Symbol.dispose, Symbol.asyncDispose, using declarations and cleanup order.
Disposable and AsyncDisposable are the two interfaces that using and await using declarations check. A value is Disposable if it has a method named by the well-known symbol Symbol.dispose; it is AsyncDisposable if it has one named by Symbol.asyncDispose that returns a promise-like value.
When the block that holds a using variable ends, in any way, the runtime calls that method. Implementing the interface lets your own classes (locks, connections, temporary files, timers) take part in this. They arrived with TypeScript 5.2 and are in the library for the esnext.disposable feature set, which Node 24 supports.
interface Disposable { [Symbol.dispose](): void; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
interface Disposable { [Symbol.dispose](): void; } interface AsyncDisposable { [Symbol.asyncDispose](): PromiseLike<void>; }
| Name | Description |
|---|---|
[Symbol.dispose]() |
Synchronous cleanup. Called when a using binding goes out of scope. |
[Symbol.asyncDispose]() |
Asynchronous cleanup. Awaited when an await using binding goes out of scope. |
Returns: nothing meaningful. The return value is ignored; for the async version the returned promise is awaited.
Edge cases:
- Disposal runs in reverse order of declaration, so the last resource acquired is released first.
nullandundefinedare allowed asusinginitializers and are skipped.- If several disposals throw, the errors are combined into a
SuppressedError. await usingcan also accept a value that only hasSymbol.dispose; it falls back to the synchronous method.
Usage notes
- Make
disposeidempotent. A second call should do nothing rather than throw. - Do not return a disposable you created with
usingfrom the same function: it is disposed on exit, before the caller can use it. Use plainconstand transfer ownership instead. DisposableStackandAsyncDisposableStackcollect several cleanups when a class owns many resources; they are themselves disposable.- Libraries often provide both: a
close()method for the old style and[Symbol.dispose]that calls it. - A
usingdeclaration cannot use destructuring (error TS1492); bind the whole value and read its properties.