Skip to content

Commit 52d7f5f

Browse files
authored
Merge pull request #46 from shinpr/feat/improve-typescript-rules-clarity
docs: Apply Japanese version improvements to English TypeScript rules
2 parents 16c7a13 + 3a17734 commit 52d7f5f

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

docs/rules-en/typescript.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
3. **Union Types・Intersection Types**: Combinations of multiple types
2222
4. **Type Assertions (Last Resort)**: Only when type is certain
2323

24+
**Type Guard Implementation Pattern**
25+
```typescript
26+
// Safely validate external input
27+
function isUser(value: unknown): value is User {
28+
return typeof value === 'object' && value !== null &&
29+
'id' in value && 'name' in value
30+
}
31+
// Usage: if (isUser(data)) { /* data is typed as User */ }
32+
```
33+
2434
**Modern Type Features**
2535
- **satisfies Operator**: Type check while preserving type inference
2636
```typescript
@@ -54,18 +64,22 @@
5464
Input Layer (`unknown`) → Type Guard → Business Layer (Type Guaranteed) → Output Layer (Serialization)
5565
5666
**Type Complexity Management**
57-
- Field Count: Up to 20 (split by responsibility if exceeded)
67+
- Field Count: Up to 20 (split by responsibility if exceeded, external API types are exceptions)
5868
- Optional Ratio: Up to 30% (separate required/optional if exceeded)
5969
- Nesting Depth: Up to 3 levels (flatten if exceeded)
6070
- Type Assertions: Review design if used 3+ times
71+
- **External API Types**: Relax constraints and define according to reality (convert appropriately internally)
6172
6273
## Coding Conventions
6374
6475
**Class Usage Criteria**
76+
- **Recommended: Implementation with Functions and Interfaces**
77+
- Rationale: Improves testability and flexibility of function composition
6578
- **Classes Allowed**:
6679
- Framework requirements (NestJS Controller/Service, TypeORM Entity, etc.)
6780
- Custom error class definitions
68-
- **Classes Prohibited**: Use functions and interfaces for everything else
81+
- When state and business logic are tightly coupled (e.g., ShoppingCart, Session, StateMachine)
82+
- **Decision Criterion**: If "Does this data have behavior?" is Yes, consider using a class
6983
```typescript
7084
// ✅ Functions and interfaces
7185
interface UserService { create(data: UserData): User }

docs/rules-ja/typescript.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
3. **ユニオン型・インターセクション型**: 複数の型の組み合わせ
2222
4. **型アサーション(最終手段)**: 型が確実な場合のみ
2323

24+
**型ガードの実装パターン**
25+
```typescript
26+
// 外部入力を安全に検証
27+
function isUser(value: unknown): value is User {
28+
return typeof value === 'object' && value !== null &&
29+
'id' in value && 'name' in value
30+
}
31+
// 使用例: if (isUser(data)) { /* dataはUser型 */ }
32+
```
33+
2434
**モダンな型機能の活用**
2535
- **satisfies演算子**: 型推論を維持しつつ型チェック
2636
```typescript
@@ -54,18 +64,22 @@
5464
入力層(`unknown`) → 型ガード → ビジネス層(型保証) → 出力層(シリアライズ)
5565
5666
**型の複雑性管理**
57-
- フィールド数: 20個まで(超えたら責務で分割)
67+
- フィールド数: 20個まで(超えたら責務で分割、外部API型は例外
5868
- オプショナル率: 30%まで(超えたら必須/任意で分離)
5969
- ネスト深さ: 3階層まで(超えたらフラット化)
6070
- 型アサーション: 3回以上使用したら設計見直し
71+
- **外部API型の扱い**: 制約を緩和し、実態に合わせて定義(内部では適切に変換)
6172
6273
## コーディング規約
6374
6475
**クラス使用の判断基準**
76+
- **推奨:関数とinterfaceでの実装**
77+
- 背景: テスタビリティと関数合成の柔軟性が向上
6578
- **クラス使用を許可**:
6679
- フレームワーク要求時(NestJSのController/Service、TypeORMのEntity等)
6780
- カスタムエラークラス定義時
68-
- **クラス使用を禁止**: 上記以外は関数とinterfaceで実装
81+
- 状態とビジネスロジックが密結合している場合(例: ShoppingCart、Session、StateMachine)
82+
- **判断基準**: 「このデータは振る舞いを持つか?」がYesならクラス検討
6983
```typescript
7084
// ✅ 関数とinterface
7185
interface UserService { create(data: UserData): User }

0 commit comments

Comments
 (0)