exhaustiveness-checking

Enforce exhaustiveness in TypeScript switch statements using assertUnreachable default branches.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill prevents runtime omissions by enforcing exhaustiveness when dealing with tagged unions in TypeScript, ensuring every union variant is explicitly handled.

Core Features & Use Cases

  • Compile-time protection against missing cases in discriminated unions.
  • Guidance on adding new variants and updating switch logic consistently.
  • Patterns like assertUnreachable to surface missing cases during development.

Quick Start

Add an assertUnreachable default in your switch statements that handle union types to force a compile-time error when a new variant is added.

Frequently Asked Questions about exhaustiveness-checking

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

FAQPage Schema
How do I enforce exhaustiveness checking in TypeScript switch statements?

To enforce exhaustiveness checking in TypeScript switch statements, add an assertUnreachable function in the default branch. This surfaces missing union variants as compile-time type errors, preventing runtime omissions when handling discriminated unions.

What does assertUnreachable do for union types in TypeScript?

The assertUnreachable pattern forces compile-time errors when a new variant is added to a discriminated union. By placing it in the default branch of a switch statement, it ensures every union case is explicitly handled, preventing runtime omissions.

How do I get a compile-time error when adding a new variant to a discriminated union?

To get a compile-time error when adding a new variant to a discriminated union, use an assertUnreachable default branch in your switch statements. This enforces exhaustiveness by surfacing missing cases as type errors during development.

When do I need exhaustiveness checking for switch statements?

You need exhaustiveness checking when handling tagged unions in TypeScript to prevent runtime omissions. It is applicable when adding new variants to discriminated unions and ensuring all cases are handled consistently in switch statements.

What is the best way to handle missing cases in TypeScript discriminated unions?

The best way to handle missing cases in TypeScript discriminated unions is using the assertUnreachable pattern in the default branch. This enforces exhaustiveness by triggering compile-time type errors for unhandled switch statement cases.