enum-driven-state-refactoring

Refactor loose state models into strict sum types like discriminated unions.

2|Updated Oct 24, 2021
One-click install
npx skills add https://github.com/jul-sh/dotfiles --skill enum-driven-state-refactoring
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: enum-driven-state-refactoring
Source: https://github.com/jul-sh/dotfiles/tree/main/dotfiles/.claude/skills/enum-driven-state-refactoring
Command: npx skills add https://github.com/jul-sh/dotfiles --skill enum-driven-state-refactoring

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps refactor code that uses optional fields or parallel booleans to represent state, making illegal states structurally impossible and improving code safety.

Core Features & Use Cases

  • State Modeling: Transforms ambiguous, optional-based state representations into strict, type-safe sum types (enums/discriminated unions).
  • Code Safety: Prevents invalid state combinations at compile time, reducing runtime errors and the need for manual validation.
  • Use Case: Refactor a UserStatus enum that currently uses optional fields like data? and error? into distinct variants like UserStatus.Loading, UserStatus.Success(Data), and UserStatus.Error(Error).

Quick Start

Refactor the provided TypeScript interface to use a discriminated union for its state.

Frequently Asked Questions about enum-driven-state-refactoring

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

FAQPage Schema
How do I refactor optional fields into a discriminated union?

To refactor optional fields into a discriminated union, you transform ambiguous optional-based state representations into strict, type-safe sum types. This ensures illegal states are structurally impossible by creating distinct variants like `Loading`, `Success`, and `Error`.

What is the best way to eliminate invalid states with sum types?

The best way to eliminate invalid states with sum types is by replacing parallel booleans and optional fields with strict enums or sealed classes. This approach makes illegal states unrepresentable at compile time, preventing runtime errors and reducing the need for manual validation.

When should I use pattern matching over conditional checks for state management?

You should use pattern matching over conditional checks when a field's validity is dependent on another field's value. Refactoring loose state models into strict sum types promotes pattern matching, ensuring type safety and preventing invalid state combinations.

How do I convert parallel booleans to type-safe enums?

To convert parallel booleans to type-safe enums, you refactor the overlapping true/false combinations into distinct variants within a sealed class or discriminated union. This makes invalid state combinations structurally impossible to represent.

Does refactoring to sealed classes prevent runtime errors?

Yes, refactoring to sealed classes prevents runtime errors by making illegal states unrepresentable at compile time. By replacing optional fields with strict sum types, the compiler enforces pattern matching exhaustiveness, eliminating invalid state combinations.

Can I refactor a TypeScript interface state model into a strict sum type?

Yes, you can refactor a TypeScript interface state model into a strict sum type by transforming optional fields like `data?` and `error?` into distinct discriminated union variants. This ensures the state model is type-safe and illegal states are structurally impossible.