{
    "slug": "typescript_using_keyword",
    "term": "using & await using (TS 5.2 Explicit Resource Management)",
    "category": "typescript",
    "difficulty": "advanced",
    "short": "TypeScript 5.2 using keyword automatically disposes resources (DB connections, file handles) when they go out of scope — like C# using or Python with.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "typescript",
        "ts52",
        "disposal",
        "resource-management"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "typescript_satisfies",
        "memory_management",
        "lfi"
    ],
    "prerequisites": [
        "typescript_generics",
        "javascript_symbols"
    ],
    "refs": [
        "https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html"
    ],
    "bad_code": "// Verbose try/finally:\nconst conn = await db.connect();\ntry {\n    await conn.query('SELECT 1');\n} finally {\n    await conn.close(); // Easy to forget\n}",
    "good_code": "// TypeScript 5.2+:\nclass DbConnection implements AsyncDisposable {\n    async [Symbol.asyncDispose]() {\n        await this.close();\n    }\n}\n\nasync function query() {\n    await using conn = await db.connect();\n    await conn.query('SELECT 1');\n} // conn.close() called automatically",
    "quick_fix": "Replace try/finally cleanup with using/await using. Implement Symbol.dispose or Symbol.asyncDispose on resource classes. Use DisposableStack for multiple resources.",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/typescript_using_keyword",
        "html_url": "https://codeclaritylab.com/glossary/typescript_using_keyword",
        "json_url": "https://codeclaritylab.com/glossary/typescript_using_keyword.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[using & await using (TS 5.2 Explicit Resource Management)](https://codeclaritylab.com/glossary/typescript_using_keyword) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/typescript_using_keyword"
            }
        }
    }
}