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

Suppressing Errors with @ts-ignore and @ts-expect-error

Selectively suppress type errors in JavaScript files using inline comments during gradual migration.

Suppressing Errors with @ts-ignore and @ts-expect-error

You'll learn how to selectively silence TypeScript errors in JavaScript files during gradual migration, and why one suppression method is safer than the other.

Why Error Suppression Exists

When migrating a large JavaScript codebase to TypeScript incrementally, you'll encounter type errors in files that aren't ready to be fully fixed yet. Rather than blocking progress, TypeScript provides inline comment directives to suppress specific errors while you work through your migration systematically.

Think of these as "I acknowledge this problem exists, but I'm choosing to ignore it for now" notes to the compiler.

The Two Suppression Comments

@ts-ignore

This comment tells TypeScript to ignore any errors on the very next line:

// @ts-ignore
const result = unknownFunction(param);

The danger? If you fix the underlying issue later, TypeScript won't warn you that the suppression comment is now unnecessary. It silently does nothing, creating clutter and false signals in your codebase.

@ts-expect-error

This is the preferred approach. It suppresses errors on the next line, but with a critical difference: if there's no error to suppress, TypeScript will complain:

// @ts-expect-error - Legacy API without types
const result = unknownFunction(param);

If you later add types for unknownFunction, TypeScript will flag the now-unnecessary @ts-expect-error comment, prompting you to remove it.

When to Use These

Acceptable scenarios:

  • Temporarily silencing errors in JavaScript files during migration
  • Working around quirks in third-party libraries without types
  • Quick prototyping when you'll return to add proper types soon

Always document WHY:

// @ts-expect-error - Third-party lib doesn't export this type correctly
import { internalHelper } from 'legacy-package';

The Safety Difference

@ts-ignore is a fire-and-forget suppression. @ts-expect-error is a self-documenting, time-limited workaround that expires automatically when the underlying problem is fixed.

Key Takeaway: Prefer @ts-expect-error over @ts-ignore during migration—it creates temporary suppressions that fail loudly when they're no longer needed, preventing dead code and outdated comments from accumulating.