typescript-best-practices
它能做什么
该技能为编写可靠的 TypeScript 代码提供规则。适合希望减少类型错误和隐藏崩溃的开发者。
适用场景
- ✓当你要求智能体编写或编辑 TypeScript 或 TSX 文件的时候。
- ✓当你希望从代码中去掉不安全的类型转换或宽松的类型的时候。
- ✓当你希望在代码依赖外部数据之前先对其进行检查的时候。
技能链接
https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请整理这个 TypeScript 文件中的类型,让它不再依赖不安全的类型转换。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会遵循一套规则,例如使用带标签的变体、对外部数据使用 unknown,并在信任其类型之前先用模式定义检查数据。这些规则使无法成立的状态无法进入代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
TypeScript best practices
Apply the type-system-discipline principle skill first.
| Rule | Summary |
|---|---|
| Discriminated unions | Model variants with a kind literal discriminant so impossible states can't be represented. No optional-field bags. |
| Branded types | Brand primitives with & { readonly __brand: "X" } so they can't be mixed up. Validate once at the boundary. A brand comes from a verifying type guard (input is AgentId), never from as. |
| Constructive modeling | Build the shape so the illegal value can't be constructed. [T, ...T[]] for non-empty, [T, T][] for even length, start plus duration for a range. Not a runtime guard, not a wish for refinement types. |
| Simplest total type | Keep T[] while every operation on it stays total. Strengthen to NonEmpty<T> only where the loose type forces !, a cast, or a "should never happen" throw. |
unknown over any |
External data is unknown. |
| Schemas before guards | Before hand-writing a property-by-property type guard, use the repository's runtime schema library and infer the type from the schema, such as z.infer. |
No as casts |
Every as is a runtime crash waiting. Cast only after validation. |
| Narrowing hierarchy | Discriminant switch > in operator > typeof/instanceof > user-defined type guard > as. |
| Type guards | Must verify the claim. A lying guard is worse than as because the bug hides behind a name that says it's safe. Name them isX or hasX. |
| Exhaustiveness | Inline const _exhaustive: never = x; in default arms so the compiler errors when a new variant is added. |
satisfies over as |
Validates the value without widening literal types. |
| Boundary validation | Parse where data crosses in, into a named domain type. Record<string, unknown> (however spelled) stops at that parse. Trust types inside. See the boundary-discipline principle skill. |
| Schema-derived types | Reach for Pick/Omit/Parameters/ReturnType/Awaited/typeof before declaring a new interface. |
| Object args | Pass objects, not positional, so argument order is self-documenting. Skip on hot paths (per-frame render, tokenizers, parsers). |
| Real tests | Don't mock what you can run. Prefer the framework's real test primitives with leak/disposable checks, and verify UI in a running build. Mock only what you can't run locally. |
| Structured telemetry | Prefer structured logger diagnostics with enough context to debug from an id. No console.log in shipped code. |
Examples: references/patterns.md.
常见问题
它适用于普通 JavaScript 文件吗?
这些规则针对 TypeScript 文件,即 .ts 和 .tsx。
什么是品牌类型?
一种标记某个值(例如 ID)的类型,使它不会与其他不同的值混淆。
为什么要避免 as 类型转换?
类型转换会告诉编译器不经检查就信任某个值,因此代码可能在运行时失败。
详情
作者的原始描述
TypeScript best practices. Use when reading or editing any .ts or .tsx file.
repo: cursor/plugins
热度
- GitHub 星标
- 15k
- 排名
- #11002
- 许可证
- —
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
advisor
15k如果你开启该功能,让你的智能体在关键节点咨询一个更强的第二模型,例如在重大决定之前,或在错误上卡住时。
check-agent-compatibility
15k全面检查 AI 编程智能体能否启动、验证和记录某个代码仓库,并返回一个兼容性评分以及主要问题。
cli-for-agents
15k教你的智能体设计或审查命令行工具,使 AI 智能体无需提示即可运行,并提供清晰的帮助文字和示例。
continual-learning
15k通过把回顾过往对话的工作交给专门的记忆更新程序,使你的 AGENTS.md 笔记保持最新。