技能 编程开发 TypeScript 最佳实践

TypeScript 最佳实践

v20260407
typescript-best-practices
在开发或审查 TypeScript 代码时约束现代规范,强调 const、显式返回、接口驱动的形状,杜绝 any、var 与忽略错误等反模式。
获取技能
100 次下载
概览

TypeScript Best Practices

When to Use

Use this skill when:

  • Writing new TypeScript code
  • Reviewing TypeScript pull requests
  • Refactoring JavaScript to TypeScript

Triggers

Activated when editing .ts or .tsx files in the project.

Rules

Always

  • Always use const for variables that won't be reassigned
  • Always use explicit return types on exported functions
  • Always prefer interface over type for object shapes

Never

  • Never use any — use unknown instead
  • Never use var — use const or let
  • Never ignore TypeScript errors with @ts-ignore

Examples

// Good: explicit return type
export function calculateTotal(items: Item[]): number {
  return items.reduce((sum, item) => sum + item.price, 0);
}
// Good: discriminated union
type Result<T> =
  | { success: true; data: T }
  | { success: false; error: Error };

Boundaries

  • Do not modify tsconfig.json without explicit permission
  • Do not add new dependencies without checking existing utilities
  • Focus only on TypeScript patterns, not runtime behavior
信息
Category 编程开发
Name typescript-best-practices
版本 v20260407
大小 1.28KB
更新时间 2026-04-14
语言