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

Dealing with Implicit any in Legacy Code

How to systematically address implicit any errors when noImplicitAny is enabled during migration.

Dealing with Implicit any in Legacy Code

You'll learn: How to systematically address implicit any errors when noImplicitAny is enabled during migration, turning type holes into type safety without grinding your migration to a halt.

The Problem: When TypeScript Sees Holes

When you enable noImplicitAny during a JavaScript-to-TypeScript migration, you'll suddenly see errors everywhere TypeScript can't infer a type. These are the "implicit any" violations—places where TypeScript would have silently treated something as any, but now you've told it to complain.

The most common culprits:

  • Function parameters without annotations
  • Empty arrays ([])
  • Variables initialized without a clear type
  • Object destructuring from external sources
  • Callback parameters in higher-order functions

The Systematic Approach

Think of this like triaging patients in an emergency room. You don't need to fix everything perfectly at once—you need to categorize and address issues by severity and impact.

Strategy 1: The Type Annotation Pass

Start with function parameters—they're the most common source of implicit any:

// Before: implicit any on 'user'
function greetUser(user) {
  return `Hello, ${user.name}`;
}

// After: explicit type
function greetUser(user: { name: string }) {
  return `Hello, ${user.name}`;
}

Strategy 2: The Empty Array Pattern

Empty arrays can't be inferred. Give them a type:

// Before: implicit any[]
const items = [];
items.push(someValue);

// After: explicit type
const items: string[] = [];
items.push(someValue);

Strategy 3: The Temporary any Escape Hatch

Sometimes you need to move forward. Use explicit any with a comment as a marker for later:

// TODO: Type this properly after UserService is converted
function processUser(user: any) {
  // ...
}

This turns an implicit any (which the compiler caught) into an explicit any (which you've acknowledged). You can now search your codebase for any and track remaining work.

Strategy 4: The Unknown Middle Ground

If you know the value is dynamic but want some safety, use unknown instead of any:

function handleData(data: unknown) {
  // Forces you to narrow before use
  if (typeof data === 'object' && data !== null) {
    // ...
  }
}

The Migration Order That Works

  1. Public API boundaries first: exported functions and classes
  2. Utility functions: they're used everywhere, so typing them helps everything else
  3. Internal implementation details last: less impact on the rest of your codebase

Key Takeaway: Converting implicit any to explicit types (or temporary explicit any) is about making type holes visible and trackable, not about achieving perfection immediately. Address the highest-impact areas first, and use explicit any with TODO comments to mark remaining work.