TypeScript Type Optimizer skill for Claude and Codex
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…
Install it once and Claude or Codex runs it for you. When your request matches, your AI loads the skill, asks for anything it is missing, and works through the template itself. Prefer to fill it in by hand? See the full prompt template.
Download the skill (.zip) · View SKILL.md
When it is used
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.
What it will ask you for
- 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)
How it works
- List every impossible state the current types allow (status plus unrelated optional fields, conflicting flags, hand-narrowed unions).
- Rewrite each as a discriminated union with a literal tag per variant and only that variant's fields; each listed state is now unrepresentable.
- Replace internal any and unknown with real types, keeping unknown only at external data boundaries, and make generic constraints explicit.
- Verify every previously valid value still assigns, and capture one call site before and after where narrowing changes.
- Write the improvement list naming the impossible state each change rules out.
What you get
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.
What it watches out for
- 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.
How to install
Claude.ai and the Claude desktop app
- Download the skill .zip.
- Open Settings, then Capabilities, and find Skills.
- Choose Upload skill and select the .zip.
- Ask Claude for help with the task. It loads the skill when your request matches.
Claude Code
- Download the skill .zip.
- Unzip it into ~/.claude/skills/ (or .claude/skills/ inside a project).
- Start a new session. Claude Code loads the skill when your request matches.
Codex
- Download the skill .zip.
- Unzip it into ~/.agents/skills/ (or .agents/skills/ inside a project).
- Codex picks it up on its own. Type $ and the skill name to use it, or just ask for the task.
Cursor
- Download the Cursor rule (.mdc).
- Save it in .cursor/rules/ inside your project.
- Cursor's agent applies the rule when your request matches its description.
AGENTS.md (Codex, Cursor, and other coding agents)
- Copy the AGENTS.md section.
- Paste it into AGENTS.md at the root of your repository, creating the file if needed.
- Coding agents that read AGENTS.md follow it when your request fits.