typescript
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTypeScript Professional
TypeScript 专业开发规范
Strict TypeScript patterns for professional development.
适用于专业开发的严格TypeScript范式。
Instructions
说明
1. Strict Mode Configuration
1. 严格模式配置
Always enable strict mode in :
tsconfig.jsonjson
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}始终在中启用严格模式:
tsconfig.jsonjson
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}2. Naming Conventions
2. 命名约定
| Type | Convention | Example |
|---|---|---|
| Interface | PascalCase, prefix with I (optional) | |
| Type | PascalCase | |
| Enum | PascalCase | |
| Function | camelCase | |
| Variable | camelCase | |
| Constant | UPPER_SNAKE_CASE | |
| 类型 | 约定 | 示例 |
|---|---|---|
| 接口 | 大驼峰命名,可前缀I(可选) | |
| 类型别名 | 大驼峰命名 | |
| 枚举 | 大驼峰命名 | |
| 函数 | 小驼峰命名 | |
| 变量 | 小驼峰命名 | |
| 常量 | 大写蛇形命名 | |
3. Type vs Interface
3. Type 与 Interface 对比
typescript
// ✅ Interface for object shapes
interface User {
id: string;
name: string;
email: string;
}
// ✅ Type for unions, intersections, primitives
type Status = 'pending' | 'active' | 'inactive';
type ApiResponse<T> = { data: T; error: null } | { data: null; error: string };typescript
// ✅ 用Interface定义对象结构
interface User {
id: string;
name: string;
email: string;
}
// ✅ 用Type定义联合类型、交叉类型、原始类型
type Status = 'pending' | 'active' | 'inactive';
type ApiResponse<T> = { data: T; error: null } | { data: null; error: string };4. Never Use any
any4. 禁止使用any
类型
anytypescript
// ❌ Bad
function process(data: any) { ... }
// ✅ Good - use unknown and narrow
function process(data: unknown) {
if (typeof data === 'string') {
return data.toUpperCase();
}
}
// ✅ Good - use generics
function process<T>(data: T): T { ... }typescript
// ❌ 不推荐
function process(data: any) { ... }
// ✅ 推荐 - 使用unknown并收窄类型
function process(data: unknown) {
if (typeof data === 'string') {
return data.toUpperCase();
}
}
// ✅ 推荐 - 使用泛型
function process<T>(data: T): T { ... }5. Utility Types
5. 工具类型
typescript
// Partial - all optional
type PartialUser = Partial<User>;
// Required - all required
type RequiredUser = Required<User>;
// Pick - select specific
type UserName = Pick<User, 'name'>;
// Omit - exclude specific
type UserWithoutId = Omit<User, 'id'>;
// Record - key-value mapping
type UserMap = Record<string, User>;typescript
// Partial - 所有属性可选
type PartialUser = Partial<User>;
// Required - 所有属性必填
type RequiredUser = Required<User>;
// Pick - 选择指定属性
type UserName = Pick<User, 'name'>;
// Omit - 排除指定属性
type UserWithoutId = Omit<User, 'id'>;
// Record - 键值对映射
type UserMap = Record<string, User>;6. Function Types
6. 函数类型
typescript
// ✅ Explicit return types for public APIs
function getUser(id: string): Promise<User | null> {
// ...
}
// ✅ Arrow function with types
const add = (a: number, b: number): number => a + b;typescript
// ✅ 公共API需显式声明返回类型
function getUser(id: string): Promise<User | null> {
// ...
}
// ✅ 带类型的箭头函数
const add = (a: number, b: number): number => a + b;7. Generics
7. 泛型
typescript
// ✅ Constrained generics
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
// ✅ Default generic types
interface ApiResponse<T = unknown> {
data: T;
status: number;
}typescript
// ✅ 带约束的泛型
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
// ✅ 带默认类型的泛型
interface ApiResponse<T = unknown> {
data: T;
status: number;
}