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

using & await using (TS 5.2 Explicit Resource Management)

TypeScript 5.2 Advanced
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list TypeScript as the only tool and mark automated as 'no', with the code_pattern being finally.*\.close\( or finally.*\.disconnect\( — meaning misuse (forgetting to use `using`, or forgetting `await using` for async cleanup) isn't caught by default linting but requires code review to spot try/finally patterns that should be converted, or runtime leaks to surface the problem.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing try/finally blocks with using/await using and implementing Symbol.dispose on resource classes — a recurring pattern replacement within a component, but not purely a one-line swap since custom resource classes need [Symbol.dispose]() implemented and async cases need [Symbol.asyncDispose](), touching a small set of related files.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope covers web and cli contexts broadly, but the burden is per-resource class: developers must remember to implement Symbol.dispose or Symbol.asyncDispose on custom resources. This is a localised convention tax — it doesn't reshape the whole codebase, but every resource abstraction must carry this obligation.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states devs wrongly believe `using` only works with built-in types, missing that any object implementing [Symbol.dispose]() qualifies. Additionally, the common_mistakes cite forgetting `await using` for async cleanup and misusing it outside block scope — multiple documented gotchas that competent developers encounter and must learn.

About DEBT scoring →

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 49
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 15 Google 6 ChatGPT 3 Ahrefs 3 Scrapy 3 Unknown AI 2 Perplexity 2 Claude 2 Bing 2 SEMrush 2 Meta AI 1 Sogou 1 PetalBot 1
crawler 36 crawler_json 6 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