1. Habit
  2. Skills
  3. Discriminated Unions skill
mkosir · 4K installs

Discriminated Unions skill
Runs by itself, TypeScript changes.

Invalid states removed from the code.

Install the Discriminated Unions skill and make it a habit: follow https://gethabit.dev/skills/mkosir/typescript-discriminated-unions.md

  1. 1Copy
  2. 2Paste into Claude Code, Codex or Cursor
  3. 3It runs by itself

4K installs784 starsMIT licenceSource on GitHub ↗

Discriminated Unions

by mkosir · 4K installs

The skill

The typescript-discriminated-unions skill applies, reviews and explains discriminated unions in TypeScript to model mutually exclusive variants, prevent invalid states and enforce exhaustiveness.

Only when you ask.

The habit

Invalid states removed from the code.

↻ Runs by itselfTypeScript changes

How the habit works.
You don't have to.

Three questionsWhen does it start? How does it check its work? What does it remember?

One run, step by step.
Then it stops.

Stop ruleStops after one change set per run, with at most 2 fix retries.

  1. 01

    Read conventions

    Your agent inspects the repository's conventions and config, which take precedence over the guidance.

  2. 02

    Find candidates

    It finds types where optional properties or boolean flags hide mutually exclusive variants.

  3. 03

    Model variants

    It suggests a discriminated union with a shared literal discriminator per variant.

  4. 04

    Keep the line

    It leaves properties optional where they may independently be absent and does not force a union.

  5. 05

    Check exhaustiveness

    It confirms switches handle every variant via the ESLint rule.

  6. 06

    Build

    It compiles with tsc and records the findings.

What it may do.
Nothing more.

Safe by defaultIt writes only in its own folder and asks you before anything goes out.

Is it for you?
Use it when it fits.

Quick checkRead both lists. If the left one sounds like you, the Discriminated Unions skill helps.

Use it when

  • ✓ modelling mutually exclusive variants
  • ✓ replacing boolean flags for app state
  • ✓ typing variant-specific props

Skip it when

  • – properties are independently optional
  • – the project is not TypeScript

Questions

Discriminated Unions skill, answered.

Does it force unions everywhere?

No. It uses a discriminated union only for mutually exclusive variants that need different data, not when a value simply changes.

Does it override my repo style?

No. Explicit repository conventions take precedence over this opinionated guidance.

How do I install the Discriminated Unions skill?

Copy the prompt at the top into your agent. It installs the skill from mkosir/typescript-style-guide and sets up the habit. Or run npx skills add mkosir/typescript-style-guide --skill typescript-discriminated-unions.

Is the Discriminated Unions skill free?

Yes. It is published under MIT. Habit is free too.

Discriminated Unions. Now as a habit.

Install the Discriminated Unions skill and make it a habit: follow https://gethabit.dev/skills/mkosir/typescript-discriminated-unions.md

  • Free
  • No account
  • Undo in one line

All mkosir skills →More code skills →

Works in

  • Claude Code
  • Codex
  • Cursor
  • ChatGPT
  • Claude app

Made by mkosir, published under MIT. Habit links to the original and never changes it. Is this your skill?

Copying didn't work here. Select the line and copy it by hand.