How To Disable Type Checking For Javascript Files In Typescript

How To Disable Type Checking For Javascript Files In Typescript

Disable type checking for Javascript files in Typescript will help you work with Typescript easier to control files and your code. One of those methods is set tsconfig.json file. So how to do it? Let’s go into detail now.

Disable type checking for Javascript files in Typescript

Set tsconfig.json

You can set the checkJs option in the tsconfig.json file to ‘false’ to disable all types of checking for the Javascript file.

Example:

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

Here setting the checkJs option with a ‘true’ value will enable all errors to be reported in all Javascript files in your project.

Use //@ts-nocheck comment

You can use the //@ts-check comment to disable all checking types in your file. And remember to put that comment at the top of your file if you want to apply for all your file.

Example:

Without //@ts-nocheck comment:

const numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const infor = {
    name: "Sherlock Holmes",
    age: 29,
    country: "England",
    address: "221B baker street"
};

const result = numberArray + infor;
console.log(result);

Follow the Javascript rule, and I cannot use a plus operator between an array and an object, so I will get an error.

The error:

index.ts:10:16 - error TS2365: Operator '+' cannot be applied to types 'number[]' and '{ name: string; age: number; country: string; address: string; }'.
10 const result = numberArray + infor;
                 ~~~~~~~~~~~~~~~~~~~
Found 1 error in index.ts:10

With //@ts-nocheck comment:

Example:

//@ts-nocheck
const numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const infor = {
    name: "Sherlock Holmes",
    age: 29,
    country: "England",
    address: "221B baker street"
};

const result = numberArray + infor;
console.log(result);

In js file:

//@ts-nocheck
var numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var infor = {
    name: "Sherlock Holmes",
    age: 29,
    country: "England",
    address: "221B baker street"
};
var result = numberArray + infor;
console.log(result);

Output:

0,1,2,3,4,5,6,7,8,9[object Object]

With the //@ts-nocheck comment, I can disable type checking and add an array with an object. As you see compiler concatenates the array with the Object.

But I recommend you disable all types of checking for Javascript files in Typescript when you know precisely what you are doing and what is the result of each code line. If not, abusing the disabled type checking method might lead to severe errors.

Example:

//@ts-nocheck
const numberArray = [0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9]
const aNumber = 10
const result = numberArray + aNumber
result.push(11)

Here I won’t get any errors when compile to the Javascript file but will get an error when executing the code.

Example:

In js file:

//@ts-nocheck
var numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var aNumber = 10;
var result = numberArray + aNumber;
result.push(11);

The error:

result.push(11);
       ^
TypeError: result.push is not a function

After adding an array with a number, it is no longer an array, so I cannot use the push method. And I get the error.

Summary

This tutorial showed how to disable type checking for Javascript files in Typescript. You can use the @ts-nocheck comment or set the ‘checkJs’ option in your tsconfig.json file. Let’s try two methods.

Maybe you are interested:

Leave a Reply

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