The .js to .ts Rename Strategy
What you'll learn: When a simple file rename from .js to .ts is sufficient versus when you need deeper refactoring based on your code's complexity.
The Simplest Migration Move
When gradually migrating JavaScript to TypeScript, your first instinct might be to simply rename app.js to app.ts and see what happens. Sometimes, that's all you need. Other times, you'll immediately face a wall of compiler errors.
The key question: Does your code's complexity match TypeScript's expectations?
When Renaming Just Works
Simple, well-structured JavaScript files often migrate with just a rename:
- Files with explicit return statements and clear function signatures
- Code using modern ES6+ patterns that TypeScript understands naturally
- Modules with straightforward imports and exports
- Code that doesn't rely heavily on dynamic behavior
Think of these files as already "TypeScript-shaped" — they just lack the explicit type annotations.
When You Need More Than a Rename
Complex JavaScript requires deeper work:
- Implicit
anyeverywhere: Functions without clear parameter patterns will triggernoImplicitAnyerrors - Dynamic property access: Code using
obj[dynamicKey]patterns needs index signatures or refactoring - Mixed-type returns: Functions returning different types based on runtime conditions need union types or overloads
- Third-party dependencies: Untyped libraries need
@typespackages or custom declarations - Prototype manipulation: Heavy use of
thisbinding or prototypal inheritance needs careful typing
The Triage Strategy
When you rename to .ts, let the compiler guide you:
- Zero errors? You're done. Move to the next file.
- A few focused errors? Fix them directly with type annotations.
- Dozens of cascading errors? Revert the rename. This file needs preparation first — perhaps extracting complex logic, adding runtime validation, or restructuring before the TypeScript conversion.
Key Takeaway: Simple rename works for straightforward code; complex, dynamic JavaScript needs refactoring before or during migration, not just a file extension change.