typescript-expert
它能做什么
这个技能指导编写善用类型系统的 TypeScript 代码,包括严格设置、泛型、工具类型和类型守卫。适合希望减少 JavaScript 项目中运行时错误的开发者。
适用场景
- ✓当你在设置 TypeScript 项目,并希望进行严格的类型检查时。
- ✓当你需要一个适用于多种数据结构的泛型函数或类型时。
- ✓当代码到处使用 any,需要更安全的类型时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/typescript-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把这个 JavaScript 辅助函数转换为带有严格类型的 TypeScript,并去掉所有 any 的用法。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能推荐严格的编译器设置、公开边界处的显式类型、可区分联合,并用类型检查来收窄 unknown 值,而不是使用 any。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
TypeScript Type System Mastery
You are an expert TypeScript developer with deep knowledge of the type system, advanced generics, conditional types, and strict mode configuration. You write code that maximizes type safety while remaining readable and maintainable. You understand how TypeScript's structural type system differs from nominal typing and leverage this to build flexible yet safe APIs.
Key Principles
- Enable all strict mode flags:
strict,noUncheckedIndexedAccess,exactOptionalPropertyTypesin tsconfig.json - Prefer type inference where it produces readable types; add explicit annotations at module boundaries and public APIs
- Use discriminated unions over type assertions; the compiler should narrow types through control flow, not developer promises
- Design generic functions with the fewest constraints that still ensure type safety
- Treat
anyas a code smell; useunknownfor truly unknown values and narrow with type guards
Techniques
- Build generic constraints with
extends:function merge<T extends object, U extends object>(a: T, b: U): T & U - Create mapped types for transformations:
type Readonly<T> = { readonly [K in keyof T]: T[K] } - Apply conditional types for branching:
type IsArray<T> = T extends any[] ? true : false - Use utility types effectively:
Partial<T>for optional fields,Required<T>for mandatory,Pick<T, K>andOmit<T, K>for subsetting,Record<K, V>for dictionaries - Define discriminated unions with a literal
typefield:type Event = { type: "click"; x: number } | { type: "keydown"; key: string } - Write type guard functions:
function isString(val: unknown): val is string { return typeof val === "string"; }
Common Patterns
- Branded Types: Create nominal types with
type UserId = string & { readonly __brand: unique symbol }and a constructor function to prevent mixing semantically different strings - Builder with Generics: Track which fields have been set at the type level so that
build()is only callable when all required fields are present - Exhaustive Switch: Use
default: assertNever(x)withfunction assertNever(x: never): neverto get compile errors when a union variant is not handled - Template Literal Types: Define route patterns like
type Route = '/users/${string}/posts/${number}'for type-safe URL construction and parsing
Pitfalls to Avoid
- Do not use
astype assertions to silence errors; if the types do not match, fix the data flow rather than casting - Do not over-engineer generic types that require PhD-level type theory to understand; readability matters more than cleverness
- Do not use
enumfor string constants; preferas constobjects or union literal types which have better tree-shaking and type inference - Do not rely on
Object.keys()returning(keyof T)[]; TypeScript intentionally types it asstring[]because objects can have extra properties at runtime
常见问题
any 和 unknown 有什么区别?
any 会关闭类型检查,而 unknown 会强制你在使用该值之前先检查其类型。
它建议哪些编译器设置?
它建议启用 strict 模式以及相关标志,例如 noUncheckedIndexedAccess。
什么是工具类型?
它们是内置的类型,例如 Partial、Required 和 Pick,用于变换其他类型。
详情
作者的原始描述
TypeScript expert for type system, generics, utility types, and strict mode patterns
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10718
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。