tagged-unions

Model complex state logic using discriminated unions with literal tags.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill tagged-unions
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tagged-unions
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/tagged-unions
Command: npx skills add https://github.com/luyi985/lyi-bash --skill tagged-unions

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Many codebases accumulate invalid state representations when using plain interfaces with optional properties. Tagging unions provides explicit relationships and safer narrowing to prevent runtime errors.

Core Features & Use Cases

  • Explicit state variants with a literal tag to enable exhaustive checks.
  • Safe narrowing through discriminated unions to guarantee correct data per state.
  • Real-world example: modeling view states, API responses, or UI modes with distinct data for each variant.

Quick Start

Create a union type with a literal 'type' field for each variant and switch on 'type' to handle every case.

Frequently Asked Questions about tagged-unions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I model complex view states in TypeScript without invalid combinations?

Model complex state logic using discriminated unions by applying a literal tag per variant. This enables compile-time narrowing to guarantee correct data per state and prevents invalid state representations.

What is the best way to ensure exhaustive handling of API response variants?

The best way to ensure exhaustive handling is to define a union type with a literal 'type' field for each API response variant. Switching on the 'type' tag enforces exhaustive checks across all distinct data states.

Why does using optional properties in TypeScript interfaces lead to runtime errors?

Optional properties lead to runtime errors because they accumulate invalid state representations. Discriminated unions provide explicit relationships and safer compile-time narrowing to prevent these runtime issues.

How do I set up discriminated unions for UI modes with distinct data requirements?

Set up discriminated unions by creating a union type containing a literal tag field for each UI mode. Apply this union to interface properties to ensure exhaustive handling and safe narrowing across variants.

Can I enforce valid state combinations in TypeScript at compile time?

Yes, you can enforce valid state combinations at compile time by using tagged unions. Defining a literal tag per variant allows compile-time narrowing to guarantee that only valid data combinations are accessible.