Migration Strategy: any to unknown
You'll learn: How to safely replace any with unknown in your codebase incrementally, improving type safety without breaking everything at once.
Why Incremental Migration Matters
Switching from any to unknown all at once is like trying to replace every window in a building simultaneously — you'll expose yourself to chaos. Instead, you want a strategy that lets you work on one section at a time while keeping the rest of the system functional.
The key insight: start at the boundaries where data enters your system, then work inward, fixing type errors as you go.
The Migration Strategy
Step 1: Identify Data Boundaries Focus on places where external data enters your codebase:
- API response handlers
- JSON parsing functions
- Event listeners
- File readers
- Database query results
Step 2: Replace any with unknown at One Boundary
Change just one function or module at a time. For example, switch a single API endpoint's return type from any to unknown.
Step 3: Fix the Resulting Type Errors
TypeScript will now complain whenever you try to use that unknown value unsafely. This is good! Add type guards, validation, or narrowing checks to make the errors disappear.
Step 4: Repeat for the Next Boundary Once one area is stable, move to the next. Your codebase becomes progressively safer.
The Pattern in Action
Think of it like replacing a leaky pipe system: you don't shut off water to the entire building. You isolate one section, replace that pipe, test it, then move to the next section. Each replacement makes the system more reliable without causing total system failure.
Key Takeaway: Replace any with unknown incrementally at data entry points, fix the type errors that surface, then move to the next boundary — gradual improvement beats risky all-at-once rewrites.