Course contentsShow
← JavaScript
Lesson 2626 of 2,72353. Type Safety Without TypeScriptFree lesson

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

Disable type checking for specific lines using // @ts-ignore or prefer // @ts-expect-error for expected issues.

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

You'll learn how to selectively disable TypeScript-style type checking on specific lines when using @ts-check in JavaScript files.

The Problem

When you enable @ts-check in your JavaScript files, the editor starts catching type errors—which is great! But sometimes you encounter false positives or need to work with legacy code that doesn't fit TypeScript's expectations. You need an escape hatch.

Two Comment Directives

// @ts-ignore

Place this comment on the line immediately before code that triggers a type error you want to suppress:

// @ts-check

let value = getSomeLegacyValue();

// @ts-ignore
value.someMethod(); // Type error ignored, even if value might not have this method

Think of @ts-ignore as saying "trust me, I know what I'm doing here."

// @ts-expect-error

This is the preferred option when you know there's a type issue:

// @ts-check

// @ts-expect-error - This API returns 'any' but will be fixed in v2
const data = legacyUntypedFunction();

The key difference: @ts-expect-error will produce a warning if there's no longer an error on that line, alerting you when the suppression becomes unnecessary.

When to Use Each

  • Use @ts-expect-error when dealing with known type issues you plan to fix or that you expect to exist
  • Use @ts-ignore sparingly, only when you're certain the code is correct despite the type checker's complaint
  • Always add explanatory comments explaining why you're suppressing the error

Key Takeaway: // @ts-expect-error is safer than // @ts-ignore because it fails loudly when the underlying error is fixed, preventing obsolete suppressions from accumulating in your codebase.