AgentSkillsCN

typescript

针对单元测试与组件测试,提供Vitest测试指南,支持现代模拟、断言与配置功能。在编写或重构Vitest测试、设置测试配置、提升测试可靠性和覆盖率,或在TypeScript与Angular项目(包括ngx-vest-forms模式)中调试易失性测试时使用此功能。

SKILL.md
--- frontmatter
name: typescript
description: TypeScript and JavaScript language expert for type-level programming,
  advanced patterns, performance optimization, error handling, and modern
  TS 5.x best practices. Use proactively for any TypeScript/JavaScript work
  including: complex type design, generics, discriminated unions, branded types,
  type guards, conditional types, template literal types, mapped types,
  satisfies operator, as const, using/await using, Result types, tsconfig
  configuration, module systems, migration strategies, and code review.
  Covers TypeScript 5.0 through 5.9+.

TypeScript Expert

Advanced TypeScript expertise focused purely on language features, type system patterns, and idiomatic JavaScript/TypeScript code. No tooling recommendations.

Core Principles

  1. Type safety over convenience — prefer unknown over any, use narrowing
  2. Leverage inference — let TypeScript infer when it produces the right type; annotate public API return types
  3. Prefer interface extends over intersections for object extension (better errors, better perf)
  4. Default to type for non-extended object shapes (no accidental declaration merging)
  5. Use satisfies to validate values without widening types
  6. Use as const for deep immutability and literal inference
  7. Branded types for domain primitives at API boundaries
  8. Discriminated unions for state modeling and error handling
  9. Exhaustive checks with never to catch unhandled cases
  10. Explicit resource management with using/await using for cleanup

Reference Files

Load these as needed based on the task:

  • Type System Patterns — Core type system: type guards, discriminated unions (incl. tuples), union tricks (string & {}, as never), branded types, conditional types, mapped types, template literals, indexed access types, deriving union types, module declarations
  • Generics & Inference — Generic patterns, const type parameters, NoInfer, inference techniques, function overloads vs generics
  • Patterns & Idiomssatisfies, as const, using/await using, Result types, error handling, ESM patterns, import organization
  • Utility Types Library — Copy-paste utility types: Brand, Result, Option, Deep*, NonEmptyArray, PathOf, exhaustive checks
  • TSConfig Reference — Recommended tsconfig for TS 5.9+, option explanations, common configurations

Quick Reference

When to Use What

NeedUse
Validate value shape without wideningsatisfies
Deep immutable config/constantsas const
Literal inference in generic function<const T> parameter
Prevent inference on specific paramNoInfer<T>
Extract union from object/array valuesIndexed access: T[keyof T], Arr[number]
Access nested type from objectT['key']['nested'] chaining
Derive per-key union from objectMapped type + [keyof T] collapse
Strip/transform object key prefixesMapped type as clause + infer
Domain-safe primitives (UserId, Email)Branded types
Autocomplete-friendly open string union'known' | (string & {})
Union-of-functions with never paramas never escape hatch
Discriminated union over tuple valuesDiscriminated tuples
State machines, API responsesDiscriminated unions
Object type extensioninterface extends
Union, intersection, conditional typestype alias
Deterministic cleanup (files, connections)using / await using
Type-safe error handlingResult<T, E> discriminated union
Runtime type narrowingType guard (is) or assertion (asserts)

Type vs Interface Decision

code
Need union, intersection, conditional, mapped, or template literal types?
  → Use `type`

Need to extend another object type?
  → Use `interface extends` (better errors + perf)

Simple object shape, no extension needed?
  → Use `type` (no accidental declaration merging)

Common Error Patterns

ErrorLikely CauseFix
"Type instantiation is excessively deep"Recursive/circular typesLimit recursion depth, use interface extends instead of &
"The inferred type of X cannot be named"Missing type export / circular depExport the type explicitly, use ReturnType<typeof fn>
"Excessive stack depth comparing types"Deep recursive typesCap recursion with conditional counter type
"Cannot find module" despite file existingWrong moduleResolutionMatch moduleResolution to your bundler

Code Review Checklist

  • No any — use unknown + narrowing, or generics
  • Return types declared for public/exported functions
  • as type assertions justified and minimal
  • Generic constraints properly bounded
  • Discriminated unions for polymorphic state
  • Exhaustive switch/if with never fallback
  • import type for type-only imports
  • No circular dependencies between modules
  • readonly / as const for data that should not mutate

TypeScript 5.x Feature Summary

VersionKey Features
5.0const type parameters, satisfies, decorators
5.2using / await using (explicit resource management)
5.4NoInfer<T>, preserved narrowing in closures
5.5isolatedDeclarations, config extends arrays
5.8--erasableSyntaxOnly (Node.js strip-types compat)
5.9import defer, expandable hovers, 11% perf from caching