Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
interface Disposable {
  [Symbol.dispose](): void;
}

Try it

TypeScript Cleanup in reverse order

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.

TypeScript await using with an async resource

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

TypeScript
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.
  • null and undefined are allowed as using initializers and are skipped.
  • If several disposals throw, the errors are combined into a SuppressedError.
  • await using can also accept a value that only has Symbol.dispose; it falls back to the synchronous method.

Usage notes

  • Make dispose idempotent. A second call should do nothing rather than throw.
  • Do not return a disposable you created with using from the same function: it is disposed on exit, before the caller can use it. Use plain const and transfer ownership instead.
  • DisposableStack and AsyncDisposableStack collect 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 using declaration cannot use destructuring (error TS1492); bind the whole value and read its properties.