principle-type-system-discipline

Applies type system patterns to make illegal states unrepresentable in statically-typed code.

1|Updated Aug 27, 2025
One-click install
npx skills add https://github.com/IgorGanapolsky/Random-Timer --skill principle-type-system-discipline-igorganapolsky
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: principle-type-system-discipline
Source: https://github.com/IgorGanapolsky/Random-Timer/tree/main/.cursor/skills/principle-type-system-discipline
Command: npx skills add https://github.com/IgorGanapolsky/Random-Timer --skill principle-type-system-discipline-igorganapolsky

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Codebases accumulate runtime failures that the compiler could have prevented: contradictory optional fields, interchangeable primitive identifiers, unchecked external data, and non-exhaustive matches. This Skill provides a set of type system design principles that shift those failures 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 using newtypes, opaque types, value classes, or branded intersections. - Boundary Parsing and Exhaustiveness: Parse untyped external data at boundaries and enforce exhaustive matching so new variants trigger compile errors. - Use Case: When reviewing a TypeScript function signature with { completed: boolean; completedAt?: Date }, apply this Skill to refactor it into { kind: 'open' } | { kind: 'done'; at: Date } so contradictory states cannot compile. ## Quick Start Review this function signature and data model using the type system discipline principles and suggest types that make illegal states unrepresentable.

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 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 primitive types like UserId and OrderId?▼

Brand semantic primitives so they are not interchangeable: use newtypes in Rust, opaque types in Swift, value classes in Kotlin, or branded intersections in TypeScript. 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, ADTs in Haskell and OCaml, and discriminated unions in TypeScript.

When should I avoid strengthening a type further?▼

Strengthen a type only where partiality appears, such as a runtime assertion or null check. If nothing would otherwise panic or fail, keep the plain type; the type system tracks cases each use site must handle, not maximum precision.

Why is exhaustive pattern matching important for sum types?▼

Exhaustive matching makes the compiler fail when a new variant is added without handling. Use a never-typed binding in TypeScript, unannotated match in Rust, or sealed-class exhaustiveness in Kotlin so future changes surface at compile time.