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

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能