add-typescript-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSetup 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
undefinedCode 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 use
const name = user.nameconst { name } = user - Instead of use
const result = await getUser(userId)const { data: user } = await getUser(userId) - Instead of use
const parseData = (data) => data.nameconst parseData = ({ name }) => name
- Instead of
- Use package for time related configuration and environment variables, instead of multiplying numbers by 1000
ms
undefined- 尽可能使用对象解构:
- 替代,使用
const name = user.nameconst { name } = user - 替代,使用
const result = await getUser(userId)const { data: user } = await getUser(userId) - 替代,使用
const parseData = (data) => data.nameconst parseData = ({ name }) => name
- 替代
- 针对时间相关的配置和环境变量,使用包,而非将数字乘以1000
ms
undefined