360 Secure SkillHub
360 安全技能中心

ts-best-practices

该 Skill 汇总 TypeScript 类型安全准则,涵盖避免 as 断言、用 unknown 替代 any、判别联合、类型收窄、类型守卫、穷尽检查和用 satisfies 保留字面量类型,并引导查看 references/patterns.md 中的示例。

poteto
热度 0.00
最近更新 2026-10-19 06:44:43
分类:开发依赖:纯提示词应用场景:代码审查 / 代码生成

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add poteto/noodle/ts-best-practices
Metadata
name
ts-best-practices
description
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.

Type Safety

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

Rule Summary
No as casts Every as is a potential runtime crash. Validate at boundaries, then cast only if earned. Prefer Zod/Valibot over manual validation.
unknown over any any disables type checking for everything it touches. External data is always unknown.
Discriminated unions Model variants with a shared literal discriminant. No optional-field bags.
Narrowing hierarchy Prefer: discriminated union switch > in operator > typeof/instanceof > type guard > as
Type guards Must actually verify the claim. Name them isX or hasX. Prefer discriminant narrowing when possible.
Exhaustiveness checks Always add default: never arm to switches over discriminated unions. Use an absurd() helper to reduce boilerplate.
satisfies over as When verifying a value matches a type without widening, use satisfies to preserve literal types.
Impossible states If 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.