跳到正文
Skillpedia
通用编程18k · #10718

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

可直接粘贴的消息

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

可以这样问 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, exactOptionalPropertyTypes in 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 any as a code smell; use unknown for 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> and Omit<T, K> for subsetting, Record<K, V> for dictionaries
  • Define discriminated unions with a literal type field: 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) with function assertNever(x: never): never to 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 as type 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 enum for string constants; prefer as const objects 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 as string[] 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能