type-checker
🎯Skillfrom cityfish91159/maihouses
Checks and fixes TypeScript type errors by identifying, analyzing, and resolving type mismatches across project files.
Installation
npx skills add https://github.com/cityfish91159/maihouses --skill type-checkerSkill Details
執行 TypeScript 類型檢查並修復類型錯誤。當遇到類型錯誤、需要類型定義、或用戶提到「type」、「類型」時使用。
Overview
# TypeScript Type Checker Skill
專門處理 TypeScript 類型檢查和類型錯誤修復。
🎯 執行時機
- 用戶提到「類型錯誤」、「type error」
- 執行
npm run typecheck發現錯誤 - 需要定義或修復 TypeScript 類型
- 代碼中出現
any需要替換為具體類型
📋 執行流程
1. 執行類型檢查
```bash
npm run typecheck
```
2. 分析錯誤輸出
TypeScript 錯誤格式:
```
src/components/Login.tsx:42:15 - error TS7006: Parameter 'user' implicitly has an 'any' type.
```
需要提取:
- 檔案路徑:
src/components/Login.tsx - 行號:
42 - 錯誤碼:
TS7006 - 錯誤訊息:
Parameter 'user' implicitly has an 'any' type
3. 閱讀相關檔案
必須閱讀的檔案(按順序):
- 錯誤所在檔案 - 理解上下文
- 相關類型定義檔案 - 檢查是否已有類型定義
```bash
# 搜尋類型定義檔案
Glob: pattern="/types//*.ts"
Glob: pattern="*/.d.ts"
```
- 相關的 interface/type - 尋找可重用的類型
```bash
# 在檔案中搜尋 interface 定義
Grep: pattern="^(export\\s+)?(interface|type)\\s+" output_mode="content"
```
4. 修復策略
#### 策略 A: 使用現有類型
```typescript
// ❌ 錯誤
function handleUser(user: any) {}
// ✅ 使用專案中已定義的類型
import { User } from "@/types/user";
function handleUser(user: User) {}
```
#### 策略 B: 定義新類型
如果專案中沒有合適的類型,在適當位置定義:
```typescript
// 在 src/types/[domain].ts 中定義
export interface UserProfile {
id: string;
name: string;
email: string;
role: "admin" | "user" | "guest";
}
```
#### 策略 C: 使用泛型
```typescript
// ❌ 錯誤
function fetchData(url: string): Promise
// ✅ 使用泛型
function fetchData
```
5. 常見類型錯誤修復
#### TS7006: 隱式 any 參數
```typescript
// ❌ 錯誤
const handleClick = (e) => {};
// ✅ 修復
const handleClick = (e: React.MouseEvent
```
#### TS2339: 屬性不存在
```typescript
// ❌ 錯誤
interface User {
name: string;
}
user.email; // Property 'email' does not exist
// ✅ 修復:擴展 interface
interface User {
name: string;
email: string;
}
```
#### TS2345: 參數類型不匹配
```typescript
// ❌ 錯誤
function greet(name: string) {}
greet(123);
// ✅ 修復:確保參數類型正確
greet(String(123));
// 或
greet(userId.toString());
```
#### TS18046: 可能為 undefined
```typescript
// ❌ 錯誤
const user = users.find((u) => u.id === id);
console.log(user.name); // 'user' is possibly 'undefined'
// ✅ 修復:加入 null check
const user = users.find((u) => u.id === id);
if (user) {
console.log(user.name);
}
// 或使用可選鏈
console.log(user?.name);
```
6. React 特定類型
```typescript
// Props 類型
interface ButtonProps {
label: string;
onClick: () => void;
disabled?: boolean;
}
// Event handlers
const handleClick = (e: React.MouseEvent
const handleChange = (e: React.ChangeEvent
const handleSubmit = (e: React.FormEvent
// State 類型
const [user, setUser] = useState
const [items, setItems] = useState
// Ref 類型
const inputRef = useRef
```
📋 修復流程
- Read 錯誤檔案
- Grep 搜尋相關類型定義
- 決定修復策略(使用現有/定義新/使用泛型)
- Edit 修復類型錯誤
- Bash 執行
npm run typecheck驗證 - 重複直到所有錯誤修復
🚨 絕對禁止
```typescript
// ❌ 永遠不要這樣做
const data: any = fetchData();
function process(input: any): any {}
// @ts-ignore
const result = riskyOperation();
```
✅ 最佳實踐
- 優先使用現有類型 - 檢查
src/types/目錄 - 類型定義集中管理 - 放在
src/types/[domain].ts - 使用嚴格模式 - 確保 tsconfig.json 開啟 strict
- Export 可重用類型 - 方便其他檔案使用
- 使用 Type Guards - 提供 runtime 類型安全
📝 回報格式
````markdown
TypeScript 類型修復報告
修復的錯誤
- src/components/Login.tsx:42
- 錯誤: TS7006 - Parameter 'user' implicitly has an 'any' type
- 修復: 使用 User interface from @/types/user
- 狀態: ✅ 已修復
- src/api/auth.ts:15
- 錯誤: TS2345 - Argument type mismatch
- 修復: 調整參數類型為 LoginCredentials
- 狀態: ✅ 已修復
驗證結果
```bash
npm run typecheck
```
````
✅ 無類型錯誤
新增的類型定義
src/types/auth.ts- LoginCredentials, AuthResponse
```
🔗 參考資源
- TypeScript 官方文檔: https://www.typescriptlang.org/docs/
- React TypeScript Cheatsheet: https://react-typescript-cheatsheet.netlify.app/
- 專案規範:
/home/user/maihouses/CLAUDE.md
```
More from this repository10
Enforces architectural best practices by guiding developers to think systematically about data flow, component boundaries, and scalability before writing code.
Enforces comprehensive testing by mandating tests for every code change, bug fix, and new feature implementation.
read-before-edit skill from cityfish91159/maihouses
Enforces strict Row Level Security (RLS) policies in Supabase, mandating default-deny access and comprehensive security checks.
Validates and sanitizes backend API inputs, preventing injection attacks and ensuring robust data integrity across server-side endpoints
Validates TypeScript/React code quality for maihouses projects, ensuring adherence to CLAUDE.md standards through comprehensive automated checks.
Enforces strict React performance guidelines by policing re-renders, optimizing bundle size, and preventing performance bottlenecks.
Maintains a persistent memory file to track project context, architecture, issues, and operational rules across AI agent sessions.
pre-commit-validator skill from cityfish91159/maihouses
skill-marketplace skill from cityfish91159/maihouses