TypeScript strict: trueへの移行で見つかった100個のエラー
## 背景
レガシープロジェクトで `strict: true` を有効にしたら100個のエラーが出た。分類と対処法。
## エラー分類
| カテゴリ | 件数 | 主な原因 |
|---------|------|---------|
| null/undefined | 45 | optional chainingの欠如 |
| any型 | 30 | 型定義の省略 |
| 暗黙のany引数 | 15 | コールバック関数の型未定義 |
| その他 | 10 | - |
## 対処パターン
### 1. null/undefined (45件)
```typescript
// Before
const name = user.profile.name;
// After
const name = user?.profile?.name ?? "Unknown";
```
### 2. any型の排除 (30件)
```typescript
// Before
function processData(data: any) { ... }
// After
interface ProcessData {
id: number;
items: Item[];
}
function processData(data: ProcessData) { ... }
```
## 結果
- 移行前: 0 type errors (strict: false)
- 移行中: 100 errors
- 移行後: 0 errors + 3つのバグを事前発見!
strict: trueで事前に見つかったバグの1つは、本番でnull参照エラーを起こしていた箇所だった。
No comments yet. Be the first to share your thoughts.