Agent Skills
› sediman-agent/OpenSkynet
› principle-type-system-discipline
principle-type-system-discipline
GitHub指导在静态类型语言中强化类型纪律,通过不可非法状态、品牌化原语、边界解析、拒绝强制转换、穷举匹配及派生权威模式,利用编译器消除运行时错误,确保类型安全。
Trigger Scenarios
设计数据类型
审查函数签名
编写静态类型代码
Install
npx skills add sediman-agent/OpenSkynet --skill principle-type-system-discipline -g -y
SKILL.md
Frontmatter
{
"name": "principle-type-system-discipline",
"description": "Apply when designing types, reviewing a function signature, or writing code in any statically-typed language. Make illegal states unrepresentable, brand semantic primitives, parse external data at boundaries, refuse to lie to the compiler, exhaust variants, derive from authoritative schemas.",
"disable-model-invocation": true
}
Type System Discipline
The type checker is a proof assistant. Use it to eliminate impossible states, mismatched primitives, and unhandled variants at compile time. Anything you let through as runtime data becomes a runtime failure the compiler could have stopped.
Applies to any typed language. Skills like typescript-best-practices ground it in specific syntax.
The patterns:
- Make illegal states unrepresentable. Model variants as sum types: discriminated unions in TypeScript, enums with payloads in Rust/Swift/Kotlin, sealed classes in Scala, ADTs in Haskell/OCaml. Don't model state as a bag of optional fields where contradictory combinations compile. A subtle anti-pattern worth naming:
{ completed: boolean; completedAt?: Date }admitscompleted: true; completedAt: undefined, which is meaningless. Derive the boolean from a single source likecompletedAt !== null, or model the variants explicitly as{ kind: 'open' } | { kind: 'done'; at: Date }. If a bug forces the question "wait, can this combination actually happen?", the type is too loose. - Brand semantic primitives.
UserIdandOrderIdare strings underneath but should not be interchangeable. Newtypes in Rust, opaque types in Swift, value classes in Kotlin, phantom types in Haskell, branded intersections in TypeScript. Validate once at creation, trust the type downstream. - 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. See the boundary-discipline principle skill for where to put validation.
- Don't lie to the type system. Casts, unsafe coercions, and assertion functions that bypass the compiler are runtime crashes waiting to happen. If the compiler can't prove a fact, prove it (validate, narrow, refine the model) or accept that the cast is a hazard. The cast you bury today is the postmortem you write next week.
- Exhaustive matching is the compiler's job. When you match on a sum type, the compiler must fail compilation if a new variant is added without handling. Use the idiom your language provides:
never-typed binding in TypeScript, unannotatedmatchin Rust,-Wincomplete-patternsin Haskell, sealed-class match exhaustiveness in Kotlin. - Derive types from authoritative schemas. When a protocol buffer, OpenAPI spec, GraphQL schema, database migration, or design-system token file defines a shape, derive from it instead of hand-rolling a parallel type. Manual duplication drifts. See the encode-lessons-in-structure principle skill.
- Prefer compile-time over runtime. Every runtime assertion, null check, and
instanceofis admitting the type system isn't carrying its weight. Push the check up to the type.
The tests:
- "Can I write a comment explaining when this combination of fields is valid?" If yes, the type is too loose. Split it into a sum type.
- "Do two of my function arguments share a primitive type but mean different things?" Brand them.
- "Where did this
any, thisas, thisassertNotNullcome from?" Trace it to the boundary and validate there instead. - "If a new variant is added next month, will the compiler tell the next agent where to add a case?" If no, the match isn't exhaustive.
- "Is this type duplicating a shape another file owns?" Derive instead.
Version History
- c9d8953 Current 2026-07-05 19:53


