{
    "slug": "typescript_utility_types",
    "term": "TypeScript Utility Types",
    "category": "typescript",
    "difficulty": "intermediate",
    "short": "Built-in generic types that transform existing types — Partial, Required, Pick, Omit, Record, Readonly, ReturnType — eliminating the need to manually duplicate type definitions.",
    "long": "TypeScript ships with utility types for common transformations: Partial<T> makes all properties optional; Required<T> makes all required; Readonly<T> makes all readonly; Pick<T,K> selects a subset of properties; Omit<T,K> removes properties; Record<K,V> creates an object type with specific key/value types; ReturnType<F> extracts a function's return type; Parameters<F> extracts parameter types. These eliminate the copy-paste type maintenance problem — derive types from a single source of truth.",
    "aliases": [
        "Partial",
        "Omit",
        "Pick",
        "Record",
        "ReturnType",
        "utility types"
    ],
    "tags": [
        "typescript",
        "types",
        "utilities"
    ],
    "misconception": "You need to manually write partial versions of types — Partial<User> is always available; writing type PartialUser = { name?: string; email?: string } is duplication.",
    "why_it_matters": "Utility types keep type definitions DRY — when User adds a field, Partial<User>, Pick<User,'name'|'email'>, and Omit<User,'password'> all update automatically.",
    "common_mistakes": [
        "Manually writing partial or pick types instead of using Partial<T> and Pick<T,K>.",
        "Using Record<string, any> when a more specific value type is known.",
        "Not using ReturnType<typeof fn> to infer return types — avoids duplication when the function signature changes.",
        "Forgetting that utility types are shallow — Partial<T> does not make nested objects partial; use DeepPartial for that."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "typescript_types",
        "typescript_generics",
        "typescript_mapped_types"
    ],
    "prerequisites": [
        "typescript_mapped_types",
        "typescript_generics",
        "typescript_interfaces_types"
    ],
    "refs": [
        "https://www.typescriptlang.org/docs/handbook/utility-types.html"
    ],
    "bad_code": "// Manually duplicated partial type — drifts when User changes:\ntype User = { id: number; name: string; email: string; role: string };\n\n// Manual copy — must be updated every time User changes:\ntype UpdateUserDTO = {\n    name?: string;\n    email?: string;\n    role?: string;\n};",
    "good_code": "// Utility types — derived, never drift:\ntype User = { id: number; name: string; email: string; role: string; password: string };\n\ntype UpdateUserDTO = Omit<Partial<User>, 'id' | 'password'>;\n// Equivalent to: { name?: string; email?: string; role?: string }\n// Automatically includes new User fields, excludes id and password\n\ntype UserPublic = Omit<User, 'password'>; // Safe for API responses\ntype UserKeys = keyof User; // 'id' | 'name' | 'email' | 'role' | 'password'",
    "quick_fix": "Use Partial<T> for optional update DTOs, Pick<T,K> for projections, Omit<T,K> to exclude sensitive fields, and ReturnType<typeof fn> to derive types from function return values",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/typescript_utility_types",
        "html_url": "https://codeclaritylab.com/glossary/typescript_utility_types",
        "json_url": "https://codeclaritylab.com/glossary/typescript_utility_types.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": "[TypeScript Utility Types](https://codeclaritylab.com/glossary/typescript_utility_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/typescript_utility_types"
            }
        }
    }
}