feat(typescript): add standards docs and sync rewrite coverage

This commit is contained in:
csh
2026-02-24 17:30:43 +08:00
parent 872c1afc24
commit 1f46203299
12 changed files with 360 additions and 12 deletions
+41
View File
@@ -0,0 +1,41 @@
# TypeScript 命名规范
## 文件与目录
- 文件名:`kebab-case.ts` / `kebab-case.tsx`
- 测试文件:`kebab-case.test.ts` / `kebab-case.spec.ts`
- 类型声明文件:`kebab-case.d.ts`
- 目录名:`kebab-case/`
## 标识符
| 类别 | 风格 | 示例 |
| ----------------- | -------------------------- | ------------------------------ |
| 类(Class | `PascalCase` | `UserService` |
| 接口(Interface | `PascalCase` | `UserProfile`(不加 `I` 前缀) |
| 类型别名(Type | `PascalCase` | `ApiResponse<T>` |
| 枚举(Enum | `PascalCase` | `HttpStatus` |
| 枚举成员 | `UPPER_WITH_UNDER` | `NOT_FOUND` |
| 函数/方法 | `camelCase` | `getUserById` |
| 变量/参数 | `camelCase` | `userId` |
| 常量(模块级) | `UPPER_WITH_UNDER` | `MAX_RETRY_COUNT` |
| 私有成员 | `_camelCase` | `_cache` |
| 泛型参数 | 单字母或 `T` 前缀 | `T``TItem``K``V` |
| React 组件 | `PascalCase` | `UserCard` |
| React Hook | `camelCase`,以 `use` 开头 | `useUserData` |
## 布尔值命名
布尔变量/属性以 `is``has``can``should` 开头:
```ts
const isLoading = true
const hasPermission = false
```
## 避免
- 单字母变量(循环索引 `i`/`j` 除外)
- 缩写(`usr``btn`);优先完整单词
- 匈牙利命名法(`strName``nCount`
- 接口名加 `I` 前缀(`IUserService`