Disable type checking for JavaScript files in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 12, 2022

banner

Photo from Unsplash

Disable type checking for JavaScript files in TypeScript #

Use the // @ts-nocheck comment to disable all type checking in a TypeScript file. If you need to disable all type checking for JavaScript files, set the checkJs option to false in your tsconfig.json file. When the option is disabled, errors are not reported in JS files.

index.ts
// 👇️ ts-nocheck disables type checking for entire file // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck // 👇️ ts-ignore ignores any ts errors on the next line // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore function multiply(a, b) { return a * b; }

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

If you're using a linter, you might have to disable the ban-ts-comment rule on the previous line.

If you need to disable all type checking for JavaScript files, you have to set the checkJs option to false in your tsconfig.json file.

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

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

By default you are only allowed to import files with .ts and .tsx extensions, but when allowJs is enabled, 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.

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

index.ts
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore function multiply(a, b) { return a * b; }

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

If you use a linter, chances are you have to disable it for the line on which you use @ts-ignore because most linters have rules against using ts comments.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.