principle-type-system-discipline
Installation
SKILL.md
Type System Discipline
Make the compiler reject impossible states, mismatched primitives, and unhandled variants. Prefer defining errors and special cases out of existence over proliferating handlers. For TypeScript syntax, see the typescript-best-practices skill.
- Make illegal states unrepresentable. Model variants as sum types, such as discriminated unions or enums with payloads. Avoid optional-field bags that permit contradictory combinations.
- Brand semantic primitives.
UserIdandOrderIdare strings underneath but should not be interchangeable. Give each its own type, such as a branded intersection in TypeScript or a newtype elsewhere. Validate once at creation, trust the type downstream. If two arguments share a primitive type but mean different things, brand them. - External data is untyped until parsed. RPC payloads, JSON, IPC messages, CLI args, config files, environment variables, database rows. Have a parse function at every boundary that turns unstructured input into the typed model. The boundary-discipline principle skill says where to put validation.
- Prove facts before asserting types. Validate or narrow values, or refine the model, instead of using casts, unsafe coercions, or assertion functions to bypass validation.
- Match variants exhaustively. Use the language's exhaustiveness check so adding a variant fails compilation until every match handles it. In TypeScript, use a
neverbinding in the default arm. - Derive types from authoritative schemas. When a protocol buffer, OpenAPI spec, GraphQL schema, database migration, design-token file, or another file owns a shape, derive from it instead of hand-rolling a parallel type. See the encode-lessons-in-structure principle skill.