Course contentsShow
← TypeScript
Lesson 1594 of 1,79637. Migrating JavaScript to TypeScriptFree lesson

The .js to .ts Rename Strategy

When to simply rename files versus when you need deeper refactoring based on code complexity.

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 any everywhere: Functions without clear parameter patterns will trigger noImplicitAny errors
  • 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 @types packages or custom declarations
  • Prototype manipulation: Heavy use of this binding or prototypal inheritance needs careful typing

The Triage Strategy

When you rename to .ts, let the compiler guide you:

  1. Zero errors? You're done. Move to the next file.
  2. A few focused errors? Fix them directly with type annotations.
  3. 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.