Skip to main content

ts-best-practices

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.

来源信息

仓库
poteto/noodle
最近来源活动
2026年3月19日 04:43
检测到的 SKILL.md 语言
英语
星标
289
分支
20

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

文件资源管理器
2 个文件

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
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](references/patterns.md) for code examples of each rule.
在 GitHub 查看