ts-google
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle TypeScript Best Practices
Google TypeScript最佳实践
Comprehensive TypeScript style guide based on Google's internal standards, designed for AI agents and LLMs. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
基于Google内部标准的全面TypeScript风格指南,专为AI Agent和大语言模型设计。包含8个类别共45条规则,按影响程度排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new TypeScript code
- Organizing modules and imports
- Designing type annotations and interfaces
- Creating classes and functions
- Reviewing code for style consistency
- Refactoring existing TypeScript code
在以下场景中参考本指南:
- 编写新的TypeScript代码
- 组织模块与导入
- 设计类型注解与接口
- 创建类与函数
- 评审代码以保证风格一致性
- 重构现有TypeScript代码
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Module Organization | CRITICAL | |
| 2 | Type Safety | CRITICAL | |
| 3 | Class Design | HIGH | |
| 4 | Function Patterns | HIGH | |
| 5 | Control Flow | MEDIUM-HIGH | |
| 6 | Error Handling | MEDIUM | |
| 7 | Naming & Style | MEDIUM | |
| 8 | Literals & Coercion | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 模块组织 | 关键 | |
| 2 | 类型安全 | 关键 | |
| 3 | 类设计 | 高 | |
| 4 | 函数模式 | 高 | |
| 5 | 控制流 | 中高 | |
| 6 | 错误处理 | 中 | |
| 7 | 命名与风格 | 中 | |
| 8 | 字面量与类型转换 | 中低 | |
Quick Reference
快速参考
1. Module Organization (CRITICAL)
1. 模块组织(关键)
- - Use named exports over default exports
module-named-exports - - Avoid mutable exports
module-no-mutable-exports - - Use ES6 modules exclusively
module-es6-modules - - Avoid TypeScript namespaces
module-no-namespaces - - Use relative paths for project imports
module-import-paths - - Use import type for type-only imports
module-import-type - - Minimize exported API surface
module-export-api-surface
- - 优先使用具名导出而非默认导出
module-named-exports - - 避免可变导出
module-no-mutable-exports - - 仅使用ES6 modules
module-es6-modules - - 避免使用TypeScript命名空间
module-no-namespaces - - 项目内部导入使用相对路径
module-import-paths - - 仅类型导入使用import type
module-import-type - - 最小化导出的API范围
module-export-api-surface
2. Type Safety (CRITICAL)
2. 类型安全(关键)
- - Never use the any type
types-no-any - - Prefer interfaces over type aliases for objects
types-prefer-interfaces - - Explicitly annotate structural types
types-explicit-structural - - Handle nullable types correctly
types-nullable-patterns - - Use consistent array type syntax
types-array-syntax - - Never use wrapper object types
types-no-wrapper-types - - Prefer Map and Set over index signatures
types-prefer-map-set - - Avoid empty object type
types-no-empty-object
- - 绝不使用any类型
types-no-any - - 定义对象类型时优先使用interface而非类型别名
types-prefer-interfaces - - 显式注解结构类型
types-explicit-structural - - 正确处理可空类型
types-nullable-patterns - - 使用一致的数组类型语法
types-array-syntax - - 绝不使用包装对象类型
types-no-wrapper-types - - 优先使用Map和Set而非索引签名
types-prefer-map-set - - 避免使用空对象类型
types-no-empty-object
3. Class Design (HIGH)
3. 类设计(高)
- - Use parameter properties for constructor assignment
class-parameter-properties - - Mark properties readonly when never reassigned
class-readonly-properties - - Use TypeScript private over private fields
class-no-private-fields - - Avoid container classes with only static members
class-no-static-containers - - Always use parentheses in constructor calls
class-constructor-parens - - Never manipulate prototypes directly
class-no-prototype-manipulation
- - 使用参数属性简化构造函数赋值
class-parameter-properties - - 永不重新赋值的属性标记为readonly
class-readonly-properties - - 使用TypeScript的private修饰符而非私有字段
class-no-private-fields - - 避免仅包含静态成员的容器类
class-no-static-containers - - 构造函数调用始终使用括号
class-constructor-parens - - 绝不直接操作原型
class-no-prototype-manipulation
4. Function Patterns (HIGH)
4. 函数模式(高)
- - Prefer function declarations over expressions
func-declarations-over-expressions - - Use concise arrow function bodies appropriately
func-arrow-concise-bodies - - Avoid rebinding this
func-avoid-this-rebinding - - Use rest parameters over arguments
func-rest-parameters - - Use correct generator function syntax
func-generator-syntax - - Use default parameters sparingly
func-default-parameters
- - 优先使用函数声明而非函数表达式
func-declarations-over-expressions - - 合理使用箭头函数的简洁体语法
func-arrow-concise-bodies - - 避免重新绑定this
func-avoid-this-rebinding - - 使用剩余参数而非arguments对象
func-rest-parameters - - 使用正确的生成器函数语法
func-generator-syntax - - 谨慎使用默认参数
func-default-parameters
5. Control Flow (MEDIUM-HIGH)
5. 控制流(中高)
- - Always use braces for control structures
control-always-use-braces - - Always use triple equals
control-triple-equals - - Prefer for-of over for-in for arrays
control-for-of-iteration - - Always include default case in switch
control-switch-default - - Avoid assignment in conditional expressions
control-no-assignment-in-condition
- - 控制结构始终使用大括号
control-always-use-braces - - 始终使用三等号进行比较
control-triple-equals - - 遍历数组时优先使用for-of而非for-in
control-for-of-iteration - - switch语句始终包含default分支
control-switch-default - - 避免在条件表达式中进行赋值操作
control-no-assignment-in-condition
6. Error Handling (MEDIUM)
6. 错误处理(中)
- - Always throw Error instances
error-throw-errors - - Type catch clause variables as unknown
error-catch-unknown - - Document empty catch blocks
error-empty-catch-comments - - Avoid type and non-null assertions
error-avoid-assertions
- - 始终抛出Error实例
error-throw-errors - - catch子句的变量类型标记为unknown
error-catch-unknown - - 为空的catch块添加注释说明
error-empty-catch-comments - - 避免使用类型断言和非空断言
error-avoid-assertions
7. Naming & Style (MEDIUM)
7. 命名与风格(中)
- - Use correct identifier naming styles
naming-identifier-styles - - Use descriptive names
naming-descriptive-names - - Avoid decorative underscores
naming-no-decorative-underscores - - No I prefix for interfaces
naming-no-interface-prefix - - Use CONSTANT_CASE for true constants
naming-constants
- - 使用正确的标识符命名风格
naming-identifier-styles - - 使用具有描述性的名称
naming-descriptive-names - - 避免使用装饰性下划线
naming-no-decorative-underscores - - 接口名称不使用I前缀
naming-no-interface-prefix - - 真正的常量使用CONSTANT_CASE命名
naming-constants
8. Literals & Coercion (LOW-MEDIUM)
8. 字面量与类型转换(中低)
- - Use single quotes for strings
literal-single-quotes - - Use correct number literal formats
literal-number-formats - - Use explicit type coercion
literal-explicit-coercion - - Avoid Array constructor
literal-array-constructor
- - 字符串使用单引号
literal-single-quotes - - 使用正确的数字字面量格式
literal-number-formats - - 使用显式类型转换
literal-explicit-coercion - - 避免使用Array构造函数
literal-array-constructor
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明及代码示例:
- 章节定义 - 类别结构与影响程度说明
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |