stand-ts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTypeScript / JavaScript Standards
TypeScript / JavaScript 编码规范
Standards for TypeScript and JavaScript code.
TypeScript与JavaScript代码的编写规范。
Package Manager
包管理器
- Prefer over
bunnpm - Use instead of
bun installnpm install - Use instead of
bun runnpm run - Use instead of
bunxnpx
- 优先使用而非
bunnpm - 使用替代
bun installnpm install - 使用替代
bun runnpm run - 使用替代
bunxnpx
Strict Mode
严格模式
- Enable in
strict: truetsconfig.json - No escape hatches without justification — if unavoidable, add a comment explaining why
any
- 在中启用
tsconfig.jsonstrict: true - 无正当理由不得使用作为“逃生舱”——若无法避免,需添加注释说明原因
any
Type Patterns
类型模式
-
Preferfor object shapes; use
interfacefor unions, intersections, and mapped typestype -
Useover
satisfiesfor type narrowing — preserves the inferred type while validating the shapeas -
Avoid— use
enumobjects instead:as consttypescript// Good const Status = { Active: "active", Inactive: "inactive", } as const; type Status = (typeof Status)[keyof typeof Status]; // Avoid enum Status { Active = "active", Inactive = "inactive", } -
Prefer discriminated unions over optional fields for state modeling
-
对象结构优先使用;联合类型、交叉类型和映射类型使用
interfacetype -
类型收窄优先使用而非
satisfies——在验证结构的同时保留推断类型as -
避免使用——改用
enum对象:as consttypescript// 推荐写法 const Status = { Active: "active", Inactive: "inactive", } as const; type Status = (typeof Status)[keyof typeof Status]; // 不推荐写法 enum Status { Active = "active", Inactive = "inactive", } -
状态建模优先使用可区分联合类型而非可选字段
Error Handling
错误处理
-
Usein catch clauses, not
unknown:anytypescript// Good catch (err: unknown) { if (err instanceof SpecificError) { ... } } // Bad catch (err: any) { ... } -
Never swallow errors with empty catch blocks
-
Prefer typed error results (pattern) over thrown exceptions for expected failure paths
Result<T, E>
-
catch语句中使用而非
unknown:anytypescript// 推荐写法 catch (err: unknown) { if (err instanceof SpecificError) { ... } } // 不推荐写法 catch (err: any) { ... } -
禁止使用空catch块吞掉错误
-
预期失败路径优先使用带类型的错误结果(模式)而非抛出异常
Result<T, E>
Imports
导入规则
- Use type-only imports for types:
import type { Foo } from "./foo"; - Avoid barrel files (re-exports) in libraries — they defeat tree-shaking and obscure dependency graphs
index.ts
- 类型仅使用类型导入:
import type { Foo } from "./foo"; - 库中避免使用桶文件(重新导出)——这会破坏摇树优化并模糊依赖关系图
index.ts
Naming
命名规范
- for types, interfaces, classes, and React components
PascalCase - for variables, functions, and methods
camelCase - for constants and environment variable names
UPPER_SNAKE_CASE - Prefix boolean variables/props with ,
is,has,shouldcan
- 类型、接口、类和React组件使用大驼峰命名
PascalCase - 变量、函数和方法使用小驼峰命名
camelCase - 常量和环境变量名使用大写蛇形命名
UPPER_SNAKE_CASE - 布尔变量/属性以、
is、has、should为前缀can
Formatting Rules
格式化规则
- More than 1 arg/param requires a trailing comma (consistent with the skill)
stand-py - Be explicit with named arguments in object parameters when more than 1 property
- 参数/形参超过1个时需添加 trailing comma(与技能保持一致)
stand-py - 对象参数包含多个属性时,显式使用命名参数
Linting
代码检查
Follow the skill for linting and formatting workflow.
lint遵循技能中的代码检查与格式化工作流。
lintTesting
测试准则
- Prefer Vitest over Jest
- Use test functions, not test classes
- Leverage blocks for grouping, not class hierarchies
describe - Use /
beforeEachfor shared setup/teardownafterEach - Use or
it.eachfor parameterized teststest.each
typescript
// Good
describe("parseConfig", () => {
it("returns defaults for empty input", () => {
expect(parseConfig({})).toEqual(defaults);
});
it.each([
{ input: "yes", expected: true },
{ input: "no", expected: false },
])("parses '$input' as $expected", ({ input, expected }) => {
expect(parseBoolean(input)).toBe(expected);
});
});- 优先使用Vitest而非Jest
- 使用测试函数,而非测试类
- 利用块进行分组,而非类层级
describe - 使用/
beforeEach处理共享的初始化/清理逻辑afterEach - 使用或
it.each实现参数化测试test.each
typescript
// 推荐写法
describe("parseConfig", () => {
it("returns defaults for empty input", () => {
expect(parseConfig({})).toEqual(defaults);
});
it.each([
{ input: "yes", expected: true },
{ input: "no", expected: false },
])("parses '$input' as $expected", ({ input, expected }) => {
expect(parseBoolean(input)).toBe(expected);
});
});React
React 约定
When working in React codebases:
- Function components only — no class components
- Prefer hooks over HOCs and render props
- Named exports for components (no )
export default - Co-locate component, styles, and tests in the same directory
- Extract custom hooks when logic is reused across components
在React代码库中开发时:
- 仅使用函数组件——禁止使用类组件
- 优先使用Hooks而非高阶组件(HOC)和渲染属性
- 组件使用命名导出(不使用)
export default - 将组件、样式和测试放在同一目录下
- 当逻辑在多个组件间复用,提取自定义Hooks