| name | typescript-refactoring |
| description | null |
Refactoring
Category: typescript · Status: 🟢 Active
When to use
Khi cải thiện kiểu trong code đã có: thu hẹp any, tách type, mà vẫn compile sạch.
Steps
- Bật/giữ strict; chạy
tsc --noEmit làm mốc, sửa cho tới khi 0 lỗi.
- Thay
any bằng unknown rồi narrow, hoặc kiểu cụ thể đúng nghĩa.
- Trích type lặp ra alias/interface dùng chung; đặt tên theo domain.
- Thu hẹp dần từng chỗ, compile lại sau mỗi bước để khoanh vùng lỗi.
- Không đổi hành vi runtime; chỉ thay đổi kiểu và tên.
Template
function handle(data: any) { return data.items.map((x: any) => x.id); }
interface Payload { items: { id: string }[]; }
function handle(data: Payload): string[] {
return data.items.map((x) => x.id);
}
Example
Good: any -> Payload interface, tsc --noEmit pass, runtime không đổi.
Avoid: Thay any bằng as SomeType ép kiểu sai sự thật, lỗi dồn xuống runtime.
Checklist