principle-type-system-discipline

Guides type design to make illegal states unrepresentable in statically-typed languages.

4|1|Updated Dec 16, 2023
One-click install
npx skills add https://github.com/Shtian/AuthentiClash --skill principle-type-system-discipline-shtian
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: principle-type-system-discipline
Source: https://github.com/Shtian/AuthentiClash/tree/main/.claude/skills/principle-type-system-discipline
Command: npx skills add https://github.com/Shtian/AuthentiClash --skill principle-type-system-discipline-shtian

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Codebases accumulate runtime failures that the compiler could have prevented: contradictory optional fields, interchangeable primitive IDs, unhandled enum variants, and unchecked external data. This Skill provides a set of type-system design principles that shift those errors from runtime to compile time. ## Core Features & Use Cases - Illegal-state elimination: Model variants as sum types (discriminated unions, enums with payloads, sealed classes) instead of bags of optional fields. - Semantic primitive branding: Distinguish types like UserId and OrderId with newtypes, opaque types, or branded intersections so they cannot be mixed up. - Boundary parsing and exhaustiveness: Parse untyped external data (JSON, RPC payloads, env vars) at boundaries and enforce exhaustive matching so new variants break compilation. - Use Case: When reviewing a function signature like { completed: boolean; completedAt?: Date }, apply the Skill to remodel it as { kind: 'open' } | { kind: 'done'; at: Date } so meaningless states no longer compile. ## Quick Start Ask the AI to review your type definitions and function signatures using the type system discipline principles to eliminate illegal states and non-exhaustive matches.

Frequently Asked Questions about principle-type-system-discipline

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

FAQPage Schema
How do I make illegal states unrepresentable in TypeScript?▼

Model variants as discriminated unions instead of objects with optional fields. For example, replace `{ completed: boolean; completedAt?: Date }` with `{ kind: 'open' } | { kind: 'done'; at: Date }` so contradictory combinations cannot compile.

How to prevent mixing up string IDs like UserId and OrderId?▼

Brand semantic primitives so they are not interchangeable: use branded intersections in TypeScript, newtypes in Rust, opaque types in Swift, or value classes in Kotlin. Validate once at creation and trust the type downstream.

Does this approach work in languages other than TypeScript?▼

Yes, the principles apply to any statically-typed language. It covers enums with payloads in Rust, Swift, and Kotlin, sealed classes in Scala, and ADTs in Haskell and OCaml, with language-specific idioms for exhaustive matching.

When should I validate external data like JSON or API responses?▼

External data is untyped until parsed, so place a parse function at every boundary: RPC payloads, JSON, CLI args, config files, environment variables, and database rows. This turns unstructured input into the typed model before it flows through the system.

When should I not strengthen a type further?▼

Strengthen a type only where partiality appears, such as a runtime assertion, null check, or a throw for cases that should never happen. If nothing would otherwise panic, keep the plain type rather than pursuing precision for its own sake.