How to Ignore Errors in TypeScript Files

TypeScript provides several special comments to instruct the TypeScript compiler to suppress specific types of errors.

Here are three ways to ignore errors in TypeScript files:

  1. @ts-ignore
  2. @ts-expect-error
  3. @ts-nocheck

Method 1: How to ignore an error with @ts-ignore

  1. The @ts-ignore comment suppresses TypeScript errors on the next line.
  2. It’s often used when you know something is correct, but TypeScript flags it as an error.
  3. Use sparingly, as it can mask genuine issues.


let x: number;

// @ts-ignore
x = "this is a string"; // TypeScript won't complain about the type mismatch.

Method 2: How to ignore an error with @ts-expect-error

  1. The @ts-expect-error comment is used when you expect the following line to have a TypeScript error.
  2. The @ts-expect-error comment was introduced in TypeScript version 3.9.
  3. It helps write tests where you want to produce a TypeScript error intentionally.
  4. If the next line doesn’t produce an error, TypeScript will raise an error saying it expected one.


let y: number;

// @ts-expect-error
y = "this is a string"; // We are explicitly saying we expect an error here.

@ts-ignore vs. @ts-expect-error

@ts-ignore and @ts-expect-error are special comments that suppress TypeScript errors but serve different purposes and behaviors. Let’s compare them.

  1. @ts-ignore is a “mute button” for TypeScript errors.
  2. @ts-expect-error is both a “mute button” and a “reminder” for TypeScript errors. It reminds you when the error you were expecting is no longer present.
  3. It’s generally advisable to use @ts-expect-error it over @ts-ignore when you plan on addressing the error in the future because it provides feedback when the issue is resolved.

Method 3: How to ignore all TypeScript compiler errors on a file

To ignore all TypeScript compiler errors in a file, add a “@ts-nocheck” comment at the top.

  1. This comment suppresses TypeScript errors for the entire file.
  2. It’s a heavy-handed approach and should be used with caution. It’s helpful to gradually migrate a JavaScript project to TypeScript when you don’t want to fix all errors simultaneously.


// @ts-nocheck
let z: number;

z = "this is a string"; // No TypeScript error, even though there's a type mismatch.

Alternatives to Ignoring TypeScript Errors

Ignoring TypeScript errors can sometimes be necessary for various reasons, such as library mismatches, gradual migrations, or temporary workarounds.

Refactoring Code

Sometimes, it’s worth refactoring a code to make it more type-safe rather than ignoring the error. This might involve restructuring data, breaking down complex functions, or using utility types.

For example, if you’re getting TypeScript errors due to incorrect types or syntax, you can refactor your code to use the correct types and follow best practices. 

Using Strict TypeScript Configuration

By enabling strict TypeScript settings, you can catch more potential errors and improve the overall quality of your code. This can help you avoid the risks of ignoring errors and make your code more robust and maintainable.

Review Compiler Options

The tsconfig.json file has various compiler options that affect type-checking. Ensure that your settings align with your project’s requirements. For example, you can adjust settings like strict, strictNullChecks, and others.

Type Assertions

You can use type assertions when you know the value type, but TypeScript isn’t.

let someValue: any = "this is a string";

let strLength: number = (someValue as string).length;

Type Definitions

If you’re using a library that doesn’t have type definitions, consider looking for or adding type definitions. The DefinitelyTyped project (@types/) is a repository for community-driven TypeScript type definitions.

npm install @types/library-name

That’s it!

Leave a Comment