Disable type checking for an entire File in TypeScript

avatar

Borislav Hadzhiev

Sat Mar 12 20222 min read

Disable type checking for an entire File in TypeScript #

Use the // @ts-nocheck comment to disable type checking for an entire file in TypeScript. The // @ts-nocheck comment instructs TypeScript to skip the file when type checking. If you use a linter, you might need to disable it on the line of the comment.

index.ts
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck console.log('no errors' / 0); console.log('no errors' / 0);

The @ts-nocheck comment disables type checking for the entire file.

If you use a linter, you might have a rule that bans using ts comments in your code. The example above shows how to disable the ban-ts-comment rule for a line.

If you only need to disable type checking for a single line, use the // @ts-ignore comment.

index.ts
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore console.log('no error' / 0);

The // @ts-ignore comment disables all type checking for the next line.

You might need to disable type checking for all the JavaScript files in your project. In that case, you have to set the checkJs option to false in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { "allowJs": true, "checkJs": false, // ... your other settings } }

The allowJs option allows you to import JavaScript files in your TypeScript project.

By default, we can only import files with .ts and .tsx extensions in our TypeScript files. Once you enable allowJs, you can also import .js and .jsx files in your TypeScript files.

When the checkJs option is enabled, errors are reported in all JavaScript files in your project.

Setting checkJs to false disables all error reporting for JavaScript files in your TypeScript project, but you are still able to import and use your Javascript files because the allowJs option is set to true.

Use the search field on my Home Page to filter through my more than 1,000 articles.