using & await using (TS 5.2 Explicit Resource Management)
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
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 13
Google 4
Unknown AI 2
Perplexity 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 19
crawler_json 3
pre-tracking 1
Related categories
⚡
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