← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

using & await using (TS 5.2 Explicit Resource Management)

typescript 5.2 Advanced

TL;DR

TypeScript 5.2 using keyword automatically disposes resources (DB connections, file handles) when they go out of scope — like C# using or Python with.

Explanation

using x = getResource() calls x[Symbol.dispose]() when the variable goes out of scope (block end, return, throw). await using x = getResource() calls x[Symbol.asyncDispose]() and awaits it. This enables deterministic cleanup without try/finally boilerplate. Implementing: add [Symbol.dispose]() to your class. Use cases: database connections, file handles, event listeners, locks, timers. The DisposableStack and AsyncDisposableStack helpers manage multiple disposables. Part of the TC39 Explicit Resource Management proposal.

Common Misconception

using only works with built-in types — any object implementing [Symbol.dispose]() works with using.

Why It Matters

using eliminates the most common source of resource leaks — forgotten cleanup in finally blocks — by making disposal automatic and scope-bound.

Common Mistakes

  • Not implementing Symbol.dispose on custom resources.
  • Forgetting await using for async cleanup (DB connections, streams).
  • Using in non-block scope — using only works in block-scoped contexts.

Code Examples

✗ Vulnerable
// Verbose try/finally:
const conn = await db.connect();
try {
    await conn.query('SELECT 1');
} finally {
    await conn.close(); // Easy to forget
}
✓ Fixed
// TypeScript 5.2+:
class DbConnection implements AsyncDisposable {
    async [Symbol.asyncDispose]() {
        await this.close();
    }
}

async function query() {
    await using conn = await db.connect();
    await conn.query('SELECT 1');
} // conn.close() called automatically

Added 23 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 2 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 13 Google 4 Unknown AI 2 Perplexity 2 ChatGPT 1 Ahrefs 1
crawler 19 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Replace try/finally cleanup with using/await using. Implement Symbol.dispose or Symbol.asyncDispose on resource classes. Use DisposableStack for multiple resources.
📦 Applies To
typescript 5.2 web cli
🔗 Prerequisites
🔍 Detection Hints
finally.*\.close\(|finally.*\.disconnect\(
Auto-detectable: ✗ No typescript
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Function Tests: Update
CWE-772

✓ schema.org compliant