How To Disable Type Checking For An Entire File In Typescript

How To Disable Type Checking For An Entire File In Typescript

Disable type checking for an entire file in Typescript will help you manipulate the Typescript engine more efficiently. You can use @ts-nocheck comment to help the code ignore the file when Typescript checks type. So how to do it? Let’s go into detail now.

Disable type checking for an entire file in Typescript

Use @ts-nocheck comment

You can use the //@ts-nocheck comment in the first line of the file to disable type checking for an entire file in Typescript. That comment will help you to skip the file when Typescript checks type.

Example:

Without //@ts-nocheck:

const animalList = [
    "Aardvark",
    "Hazel Dormouse",
    "Huntsman Spider",
    "Song Thrush",
    "Stag Beetle",
    "Stellers Sea Cow"
];

const anNumber = 9;
const result = animalList + anNumber;

The error happens when I try to convert to a js file:

index.ts:3:16 - error TS2365: Operator '+' cannot be applied to types 'string[]' and 'number'.
3 const result = animalList + anNumber                
~~~~~~~~~~~~~~~~~~~~~
Found 1 error in index.ts:3

With //@ts-nocheck:

//@ts-nocheck
const animalList = [
    "Aardvark",
    "Hazel Dormouse",
    "Huntsman Spider",
    "Song Thrush",
    "Stag Beetle",
    "Stellers Sea Cow"
];

const anNumber = 9;
const result = animalList + anNumber;
console.log(result);

Convert to js file success:

//@ts-nocheck
var animalList = [
    "Aardvark",
    "Hazel Dormouse",
    "Huntsman Spider",
    "Song Thrush",
    "Stag Beetle",
    "Stellers Sea Cow"
];
var anNumber = 9;
var result = animalList + anNumber;
console.log(result);

Output:

Aardvark, Hazel Dormouse, Huntsman Spider, Song Thrush, Stag Beetle, Stellers Sea Cow9

But remember //@ts-nocheck comment will only work while using the compiler file, not when executing the file. That means you can still get the error if your logic code is wrong or you cannot control your result code.

Example:

//@ts-nocheck
const anString = "Hello From Learn Share IT";
anString.push("Hello World");

I still can convert to a js file. But if I try to execute the code, it will get the error.

The error:

anString.push('Hello World');
         ^
TypeError: anString.push is not a function

Here error happens because I try to use the push method, which doesn’t support string type.

Set tsconfig.json file

If you want to disable type checking for all the Javascript files in your project, you can set the checkJs option to false.

Example:

{
    // Some code line
    "compilerOptions": {
        "checkJs": false,
        // Some code line
    }
}

Here checkJs option with a false value will help you disable all error reports for Javascript files.

Summary

In this article, we show you how to disable type checking for an entire file in Typescript. You can use //ts-nocheck and set the “checkJs” option to false in the tsconfig.json file. We hope it helpful to you.

Maybe you are interested:

Leave a Reply

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