add-typescript-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Setup TypeScript Best Practices

配置TypeScript最佳实践

Create or update CLAUDE.md in with following content, <critical>write it strictly as it is<critical>, do not summaraise or introduce and new additional information:
markdown
undefined
在CLAUDE.md中创建或更新以下内容,<critical>严格按照原文编写,不要总结或添加任何新信息<critical>
markdown
undefined

Code Style Rules

代码风格规则

General Principles

通用原则

  • TypeScript: All code must be strictly typed, leverage TypeScript's type safety features
  • TypeScript:所有代码必须严格类型化,充分利用TypeScript的类型安全特性

Code style rules

代码风格细则

  • Interfaces over types - use interfaces for object types
  • Use enum for constant values, prefer them over string literals
  • Export all types by default
  • Use type guards instead of type assertions
  • 优先使用Interfaces而非Types - 针对对象类型使用接口
  • 针对常量值使用enum,优先于字符串字面量
  • 默认导出所有类型
  • 使用类型守卫而非类型断言

Best Practices

最佳实践

Library-First Approach

优先使用库的开发方式

  • Common areas where libraries should be preferred:
    • Date/time manipulation → date-fns, dayjs
    • Form validation → joi, yup, zod
    • HTTP requests → axios, got
    • State management → Redux, MobX, Zustand
    • Utility functions → lodash, ramda
  • 建议优先使用库的常见场景:
    • 日期/时间处理 → date-fns, dayjs
    • 表单验证 → joi, yup, zod
    • HTTP请求 → axios, got
    • 状态管理 → Redux, MobX, Zustand
    • 工具函数 → lodash, ramda

Code Quality

代码质量

  • Use destructuring of objects where possible:
    • Instead of
      const name = user.name
      use
      const { name } = user
    • Instead of
      const result = await getUser(userId)
      use
      const { data: user } = await getUser(userId)
    • Instead of
      const parseData = (data) => data.name
      use
      const parseData = ({ name }) => name
  • Use
    ms
    package for time related configuration and environment variables, instead of multiplying numbers by 1000
undefined
  • 尽可能使用对象解构:
    • 替代
      const name = user.name
      ,使用
      const { name } = user
    • 替代
      const result = await getUser(userId)
      ,使用
      const { data: user } = await getUser(userId)
    • 替代
      const parseData = (data) => data.name
      ,使用
      const parseData = ({ name }) => name
  • 针对时间相关的配置和环境变量,使用
    ms
    包,而非将数字乘以1000
undefined