TypeScript Development Guide
This skill provides mandatory specifications for writing TypeScript in this project to ensure consistency, type safety, and code cleanliness.
1. Naming Conventions
1.1 Variables and Functions
- Variables: Use
snake_case (e.g., theme_value, is_dev).
- Functions: Use
camelCase (e.g., getAntdTheme, setGlobalAnimation).
- Internal Props Objects: Use
props_* prefix (e.g., props_side_bar).
1.2 Classes and Types
- Classes: Use
PascalCase (e.g., GlobalModel, Settings).
- Interfaces/Types: Use
PascalCase. Interfaces preferably start with I (e.g., IProps, IPropsSidebar).
- Enum-like Types: Use literal union types instead of
enum where possible (e.g., type Theme = 'light' | 'dark' | 'system').
2. Type Definitions
2.1 File Organization
- Global Types: Place in
packages/app/types/ or packages/app/typings/.
- Local Types: Use
types.ts file in the component or module directory.
- Props: Define an
IProps interface for each component, typically in the component file or adjacent types.ts.
2.2 Best Practices
- Explicit Types: Prefer providing explicit type annotations for function parameters and complex return value types.
- Utility Types: Leverage TypeScript's utility types (
Pick, Omit, Partial, Exclude) to reuse existing definitions.
- Strict Types: Avoid
any. If the type is truly unknown, use unknown and perform type casting (cast) when necessary.
- Type Alias vs. Interface: Use
interface for object structures (especially props), use type for unions, intersections, or base types.
3. Code Style
3.1 Imports and Exports
- Named Exports: Utility functions and models prefer named exports.
- Default Exports: For the main component or main class in a file, use default export.
- Pure Type Imports: Use
import type for importing types to maintain clear process boundaries and reduce bundle size.
- Path Aliases: Use
@/ to reference the src directory (e.g., import { Settings } from '@/models').
3.2 Statements and Logic
- Arrow Functions: Component definitions and small utility functions prefer arrow functions.
- Destructuring: Use destructuring for props and objects to improve readability.
- Conditional Logic: Use optional chaining (
?.) and nullish coalescing (??) for safer property access.
3.3 Code Spacing and Blank Lines
Use blank lines to separate code with different execution styles or visual appearances. If two adjacent lines of code look stylistically different, a blank line must be added between them.
When to Add Blank Lines:
- Between data fetching and return statements
- Between variable calculation and usage
- Between multiple consecutive operations (different independent steps)
- Before early returns
- Between different operation types (sync vs async, query vs mutation)
- Before and after state changes
Recommended:
async getSnapshot(weight_threshold = 0.2) {
const nodes = await this.query(sql.sql_get_snapshot_nodes(weight_threshold))
const edges = await this.query(sql.sql_get_snapshot_edges(weight_threshold))
return { nodes, edges }
}
async tick(threshold_override?: number) {
const threshold = threshold_override ?? 0.5
await this.exec(sql.sql_tick(threshold))
}
Avoid:
async getSnapshot(weight_threshold = 0.2) {
const nodes = await this.query(sql.sql_get_snapshot_nodes(weight_threshold))
const edges = await this.query(sql.sql_get_snapshot_edges(weight_threshold))
return { nodes, edges }
}
4. Summary Checklist
1---2name: typescript3description: Guide for implementing TypeScript in projects, including naming conventions, type definitions, and code style. Triggered when writing or refactoring TypeScript code.4---56# TypeScript Development Guide78This skill provides mandatory specifications for writing TypeScript in this project to ensure consistency, type safety, and code cleanliness.910## 1. Naming Conventions1112### 1.1 Variables and Functions1314- **Variables**: Use `snake_case` (e.g., `theme_value`, `is_dev`).15- **Functions**: Use `camelCase` (e.g., `getAntdTheme`, `setGlobalAnimation`).16- **Internal Props Objects**: Use `props_*` prefix (e.g., `props_side_bar`).1718### 1.2 Classes and Types1920- **Classes**: Use `PascalCase` (e.g., `GlobalModel`, `Settings`).21- **Interfaces/Types**: Use `PascalCase`. Interfaces preferably start with `I` (e.g., `IProps`, `IPropsSidebar`).22- **Enum-like Types**: Use literal union types instead of `enum` where possible (e.g., `type Theme = 'light' | 'dark' | 'system'`).2324## 2. Type Definitions2526### 2.1 File Organization2728- **Global Types**: Place in `packages/app/types/` or `packages/app/typings/`.29- **Local Types**: Use `types.ts` file in the component or module directory.30- **Props**: Define an `IProps` interface for each component, typically in the component file or adjacent `types.ts`.3132### 2.2 Best Practices3334- **Explicit Types**: Prefer providing explicit type annotations for function parameters and complex return value types.35- **Utility Types**: Leverage TypeScript's utility types (`Pick`, `Omit`, `Partial`, `Exclude`) to reuse existing definitions.36- **Strict Types**: Avoid `any`. If the type is truly unknown, use `unknown` and perform type casting (cast) when necessary.37- **Type Alias vs. Interface**: Use `interface` for object structures (especially props), use `type` for unions, intersections, or base types.3839## 3. Code Style4041### 3.1 Imports and Exports4243- **Named Exports**: Utility functions and models prefer named exports.44- **Default Exports**: For the main component or main class in a file, use default export.45- **Pure Type Imports**: Use `import type` for importing types to maintain clear process boundaries and reduce bundle size.46- **Path Aliases**: Use `@/` to reference the `src` directory (e.g., `import { Settings } from '@/models'`).4748### 3.2 Statements and Logic4950- **Arrow Functions**: Component definitions and small utility functions prefer arrow functions.51- **Destructuring**: Use destructuring for props and objects to improve readability.52- **Conditional Logic**: Use optional chaining (`?.`) and nullish coalescing (`??`) for safer property access.5354### 3.3 Code Spacing and Blank Lines5556Use blank lines to separate code with different execution styles or visual appearances. **If two adjacent lines of code look stylistically different, a blank line must be added between them.**5758**When to Add Blank Lines:**5960- Between data fetching and return statements61- Between variable calculation and usage62- Between multiple consecutive operations (different independent steps)63- Before early returns64- Between different operation types (sync vs async, query vs mutation)65- Before and after state changes6667**Recommended:**6869```typescript70async getSnapshot(weight_threshold = 0.2) {71 const nodes = await this.query(sql.sql_get_snapshot_nodes(weight_threshold))72 const edges = await this.query(sql.sql_get_snapshot_edges(weight_threshold))7374 return { nodes, edges }75}7677async tick(threshold_override?: number) {78 const threshold = threshold_override ?? 0.57980 await this.exec(sql.sql_tick(threshold))81}82```8384**Avoid:**8586```typescript87async getSnapshot(weight_threshold = 0.2) {88 const nodes = await this.query(sql.sql_get_snapshot_nodes(weight_threshold))89 const edges = await this.query(sql.sql_get_snapshot_edges(weight_threshold))90 return { nodes, edges }91}92```9394## 4. Summary Checklist9596- [ ] Are variable names `snake_case`?97- [ ] Are function names `camelCase`?98- [ ] Are component props using `IProps` or `IProps*`?99- [ ] Are pure type imports using `import type`?100- [ ] Are local types placed in `types.ts` files?101- [ ] Has `any` been avoided in favor of strict types or `unknown`?102- [ ] Are blank lines used to separate different execution style code blocks?