360 Secure SkillHub
360 安全技能中心

principle-type-system-discipline

该技能以原则清单形式指导静态类型语言中的类型设计:用和类型排除非法状态,为语义原始值加品牌类型,在边界解析外部数据,避免断言绕过编译器,并要求穷尽匹配与从权威 schema 派生类型。适用于类型设计、函数签名审查和编码,主要输出文字建议,不包含具体语言实现或编译器执行。

分类:开发
mdsmithaustin
热度 0.00
最近更新 2026-11-26 11:31:41

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add mdsmithaustin/pstack/principle-type-system-discipline
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
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.

Type System Discipline

Make the compiler reject impossible states, mismatched primitives, and unhandled variants.

  • Make illegal states unrepresentable. Model variants as sum types (discriminated unions, enums with payloads, sealed classes), not as a bag of optional fields where contradictory combinations compile. { completed: boolean; completedAt?: Date } admits completed: true with no date. Derive the boolean from completedAt, or model { kind: 'open' } | { kind: 'done'; at: Date }. If you can write a comment explaining when a combination of fields is valid, split it into a sum type.
  • Types are constructions, not restrictions. Build the type up from the values you want instead of carving them out of a looser type with checks. A non-empty list is a head plus a rest. A valid time range is a start plus a duration. Choose the shape that cannot build the illegal value and expose the interface callers need on top.
  • Brand semantic primitives. If two arguments share a primitive type but mean different things, give each its own type. Validate once at creation.
  • External data is untyped until parsed. JSON, RPC payloads, CLI args, config, environment variables, and database rows go through a parse function at the boundary that returns the typed model.
  • Don't lie to the type system. Casts, unsafe coercions, and assertion functions that bypass the compiler are latent runtime crashes. If the compiler can't prove a fact, prove it (validate, narrow, refine the model) or accept that the cast is a hazard. Trace each any, as, or assertNotNull to the boundary and validate there.
  • Exhaustive matching is the compiler's job. A match on a sum type must fail compilation when a new variant is added without handling, so the compiler tells the next agent where to add a case. Use your language's idiom: a never-typed binding in TypeScript, an unannotated match in Rust, -Wincomplete-patterns in Haskell, sealed-class match exhaustiveness in Kotlin.
  • Derive types from authoritative schemas. When a schema file owns a shape, derive the type from it instead of hand-rolling a parallel one. Call the Skill tool with principle-encode-lessons-in-structure.
  • Strengthen a type only where partiality appears. A runtime assertion, null check, or "this should never happen" throw marks the place a type is too weak. Push that check up into the type. Then stop. The type system's job is to track the cases each use site must handle, not to describe the data as precisely as possible. Prefer total functions. sum of an empty list is 0, so it takes the plain list. head of an empty list has no answer, so it demands the non-empty one. If nothing would otherwise panic, keep the plain type.

For TypeScript syntax, call the Skill tool with typescript-best-practices.