ts-best-practices

v2026.09.24

TypeScript type safety guidelines for writing maximally type-safe code. Apply these patterns when writing or reviewing any TypeScript: discriminated unions, type narrowing, type guards, exhaustiveness checks, avoiding `as` casts, preferring `unknown` over `any`, and making impossible states unrepresentable. Use this skill whenever writing TypeScript code, reviewing TypeScript for type safety issues, or when the user mentions type safety, type narrowing, discriminated unions, or asks to make types stricter/more explicit.

GitHub
安装命令
npx skhub add poteto/ts-best-practices
Markdown
SKILL.md

Type Safety

This project's TypeScript policy. Apply when writing or reviewing TypeScript.

RuleSummary
No as castsEvery as is a potential runtime crash. Validate at boundaries, then cast only if earned. Prefer Zod/Valibot over manual validation.
unknown over anyany disables type checking for everything it touches. External data is always unknown.
Discriminated unionsModel variants with a shared literal discriminant. No optional-field bags.
Narrowing hierarchyPrefer: discriminated union switch > in operator > typeof/instanceof > type guard > as
Type guardsMust actually verify the claim. Name them isX or hasX. Prefer discriminant narrowing when possible.
Exhaustiveness checksAlways add default: never arm to switches over discriminated unions. Use an absurd() helper to reduce boilerplate.
satisfies over asWhen verifying a value matches a type without widening, use satisfies to preserve literal types.
Impossible statesIf a bug requires asking "can this combination happen?" the type is too loose. Tighten it.

Read references/patterns.md for code examples of each rule.

发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

2026年9月24日

分类

未分类

许可证

MIT

源路径

.agents/skills/ts-best-practices

默认分支

main

最新提交

82d2921

Tree SHA

9fccb6b