---
name: typescript-type-optimizer
description: "Rewrites wide unions and loose object types as discriminated unions and precise aliases, so the compiler narrows for you and the runtime checks you kept writing can go. Use when TypeScript types allow impossible states, wide unions get narrowed by hand everywhere, or any has crept in, and you want the compiler to catch more. Do not use for mechanical renames and deprecation removal, generating types from data, or runtime validation. Related topics: coding, typescript."
---

# TypeScript Type Optimizer

Rewrites wide unions and loose object types as discriminated unions and precise aliases, so the compiler narrows for you and the runtime checks you kept writing can go.

## When to use

Use this skill when TypeScript types allow impossible states, wide unions get narrowed by hand everywhere, or any has crept in, and you want the compiler to catch more. Do not use it for mechanical renames and deprecation removal, generating types from data, or runtime validation.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. List every impossible state the current types allow (status plus unrelated optional fields, conflicting flags, hand-narrowed unions).
3. Rewrite each as a discriminated union with a literal tag per variant and only that variant's fields; each listed state is now unrepresentable.
4. Replace internal any and unknown with real types, keeping unknown only at external data boundaries, and make generic constraints explicit.
5. Verify every previously valid value still assigns, and capture one call site before and after where narrowing changes.
6. Write the improvement list naming the impossible state each change rules out.

## Inputs

- **Type definitions** (longer free text)
- **Scope** (choose one: Single interface, Module exports, Global namespace)
- **Complexity** (choose one: Simple, Moderate, Complex)
- **Preferred style** (choose one: Interfaces, Type aliases, Mixed)
- **Strictness** (choose one: Standard, Strict null checks, Strict with exactOptionalPropertyTypes)
- **Existing constraints** (longer free text)

## Output

A TypeScript code block with the refactored types, a bulleted list of improvements each naming the state it rules out, and before-and-after call-site snippets where needed.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### TypeScript Type Optimizer

Tighten the type definitions below so more errors fail at compile time.

- Type definitions: {{Type definitions}}
- Scope: {{Scope}}
- Complexity: {{Complexity}}
- Preferred style: {{Preferred style}}
- Strictness: {{Strictness}}
- Existing constraints: {{Existing constraints}}

Find the places where the types allow states the code never intends: a status string plus optional fields that only make sense for some statuses, boolean flags that cannot both be true, or a wide union that every consumer narrows by hand. Convert each to a discriminated union with one literal tag per variant and only that variant's fields, so narrowing is a switch on the tag. Replace any and loose unknown at internal boundaries with real types; keep unknown only where external data enters. Make generic constraints explicit and document what each parameter stands for.

Prefer the user's style, but recommend type aliases where mapped or conditional types are involved, and keep interfaces where declaration merging or extends is in play. Do not change runtime behaviour: every rewrite must keep existing valid values assignable. Where a change would require edits at call sites, show one representative call site before and after.

### Output
Return the refactored types in one TypeScript code block, then a bulleted list of the specific safety improvements (the impossible state each one rules out), then any call-site changes required. Imports only when they are part of the refactor.

## Pitfalls

- Adding a discriminant without a literal type, so the tag is string and nothing narrows.
- Converting an interface that other code extends or merges into a type alias, which breaks those declarations.
- Tightening a type so that data already in production no longer assigns, turning a type fix into a migration.
