跳到正文
Skillpedia
通用编程15k · #10991

principle-type-system-discipline

它能做什么

为智能体提供一条设计类型、尽早发现错误的规则。适合使用静态类型语言的开发者。

适用场景

  • ✓当你设计类型或审阅函数签名的时候。
  • ✓当两个值共享同一类型,却代表不同含义的时候。
  • ✓当来自程序外部的数据进入代码的时候。

技能链接

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-type-system-discipline/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-type-system-discipline/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我们的用户 ID 和订单 ID 都是普通字符串,经常被混淆。请收紧类型,让编译器阻止这种情况。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会为每个语义值赋予自己的类型,在边界处把外部数据解析为带类型的模型,让未处理的变体导致构建失败,并从权威的模式定义中推导类型,而不是手写副本。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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.

  • Brand semantic primitives. UserId and OrderId are 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.
  • 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.
常见问题

什么是品牌类型?

一种底层看似字符串、却不能与其他 ID 混用的类型,这样编译器就能捕获这类错误。

解析发生在哪里?

在每个边界处,例如网络负载、配置文件、命令行参数和数据库行。

为什么要从模式定义中推导类型?

这样由协议文件或迁移定义的结构就不会被手工复制而逐渐产生偏差。

详情

作者的原始描述

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, exhaust variants, derive from authoritative schemas.

repo: cursor/plugins

热度

GitHub 星标
15k
排名
#10991
许可证
—
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能