principle-type-system-discipline

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

6.6k|542|Updated Jan 23, 2026
One-click install
npx skills add https://github.com/cursor/plugins --skill principle-type-system-discipline
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: principle-type-system-discipline
Source: https://github.com/cursor/plugins/tree/main/pstack/skills/principle-type-system-discipline
Command: npx skills add https://github.com/cursor/plugins --skill principle-type-system-discipline

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Codebases accumulate runtime failures that the compiler could have prevented: contradictory optional fields, interchangeable primitive IDs, unchecked casts, and non-exhaustive matches. This Skill provides a set of type design principles that shift those errors from runtime to compile time in any statically-typed language.

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, or branded intersections so they cannot be mixed up.
  • Boundary Parsing and Exhaustiveness: Parse external data (JSON, RPC, env vars) into typed models 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 contradictory states cannot compile.

Quick Start

Ask the agent to review this type definition and function signature using the type system discipline principles and suggest how to 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 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 primitive 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, then trust the type downstream.

Does this approach work in languages other than TypeScript?

Yes, the principles apply to any statically-typed language. It maps each pattern to language idioms: enums with payloads in Rust and Swift, sealed classes in Scala and Kotlin, and ADTs in Haskell and OCaml.

When should I avoid strengthening a type further?

Strengthen a type only where partiality actually appears, such as a runtime assertion or null check. If nothing would otherwise panic, keep the plain type, since extra precision costs reuse and ceremony without buying safety.

Why are type casts and assertions considered risky?

Casts and assertion functions bypass the compiler's proof, turning checkable facts into runtime crashes. Instead, validate and narrow at the boundary where untyped data enters, or refine the model so the compiler can prove the fact.