How To Disable Type Checking For A Line In Typescript

How To Disable Type Checking For A Line In Typescript

To disable type checking for a line in Typescript, you can choose one of these methods: Use @ts-ignore comment, Use @ts-expect-error or use Typescript-ESLint. So how to do it? Let’s go into detail now.

Disable type checking for a line in TypeScript

Use @ts-ignore comment

To disable type checking for a line in Typescript, you can use the @ts-ignore comment. That comment will help you to disable type checking for the following line.

Example:

const result = "Hello From Learn Share IT" % 10;
console.log(result);

When I try to convert the ts file to js file, I will get an error because the remainder operator cannot use between string and number. 

The error:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

1 const result = "Hello From Learn Share IT" % 10;
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~

With the @ts-ignore comment, I can disable type checking for that line of code.

Example:

//@ts-ignore
const result = "Hello From Learn Share IT" % 10;
console.log(result);

Then I can convert the ts file to a js file.

//@ts-ignore
var result = "Hello From Learn Share IT" % 10;
console.log(result);

But of course, it is still not a valid value, so I will get NaN.

Output:

NaN

Use @ts-expect-error

With the @ts-expect-error comment, the line will make your compiler expect an error. That means the error only occurs when your code is correct.

Example:

//@ts-expect-error
const result = "Hello From Learn Share IT" % 10;
console.log(result);

Output:

NaN

With Typescript-ESLint

If you are using Typescript-ESLint, you can ignore the next line by this command:

eslint-disable-next-line

But it only disables the eslint rule for the following line, not with the ts or js rule.

Example:

//eslint-disable-next-line
const result = "Hello From Learn Share IT" % 10;
console.log(result);

I still get the error with “tsc”.

The error:

index.ts:2:16 - error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
2 const result = "Hello From Learn Share IT" % 10;
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~

So you can combine the eslint-disable-next-line comment with the @ts-ignore or @ts-expect-error comment if you are working with Typescript-ESLint.

Example:

  //eslint-disable-next-line
  //@ts-ignore
  const result = "Hello From Learn Share IT" % 10;
  console.log(result)

Output:

NaN

With entire file

If you want to disable multiple files, you can re-use the @ts-ignore comment.

Example:

//@ts-ignore
const aString = "Hello World";

//@ts-ignore
const aNumber = 10;

//@ts-ignore
const result = aString % aNumber;
console.log(result);

Output: 

NaN

But it might be cumbersome if you have many line codes and want to disable type checking. So with it, you can use the @ts-nocheck comment at the top of your file to disable type checking for the entire file.

Example:

//@ts-nocheck
const aString = "Hello World";
const aNumber = 10;
const result = aString % aNumber;
console.log(result);

Output:

NaN

But always remember to be aware of how the code works and the result of your code. If not, it can lead to serious errors.

Summary

In this article, I showed you how to disable type checking for a line in Typescript. You can use the @ts-ignore or @ts-expect-error comment. Or if you have much of the line you want to disable type checking, you can use the @ts-nocheck comment.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *