Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript checkJs option

Reference for checkJs: reporting type errors in JavaScript files, JSDoc types, per-file @ts-check and @ts-nocheck comments and how to adopt it gradually.

checkJs makes the compiler report type errors in JavaScript files, using the same rules as for TypeScript. Types come from inference and from JSDoc comments such as @param {string} name. It is a way to get much of TypeScript's safety without renaming a single file.

It works together with allowJs, which is what pulls JavaScript files into the project in the first place. If you only want checking in some files, leave the option off and write // @ts-check at the top of those files. If you turn the option on for everything, you can switch single files off with // @ts-nocheck.

JSON
{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "noEmit": true
  }
}

Try it

TypeScript @ts-expect-error on a single line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript @ts-nocheck turns the checker off

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "allowJs": true, "checkJs": true } }
Part Description
Value true or false. Default: false. Needs allowJs.
// @ts-check First comment lines of a JavaScript file: check this file even when checkJs is off.
// @ts-nocheck Do not check this file even when checkJs is on.
// @ts-ignore Skip errors on the next line.
// @ts-expect-error Expect an error on the next line; report if there is none.

Edge cases: JavaScript files cannot contain type annotation syntax (x: number), so all types go in JSDoc comments. The compiler is more lenient inside JavaScript: for example, a function parameter without a JSDoc type is an implicit any, reported only when noImplicitAny is enabled.

Usage notes

  • Adopt per directory. Start with // @ts-check in a few files, then enable checkJs and add // @ts-nocheck to files you are not ready for. The list of @ts-nocheck files is your migration to-do list.
  • JSDoc supplies types. @param {number} n, @returns {string}, @type {Map<string, number>}, @typedef and @template T cover most needs; see JSDoc type annotations.
  • Same strictness switches. strict and the other type-checking options apply to JavaScript files too, so a strict project reports more in .js than a lenient one.
  • Common false alarms. Properties added to functions or objects after creation, and module.exports patterns, may need a JSDoc @type or a @typedef to describe them.
  • Ignored libraries. Declaration files and node_modules are not type-checked as your code; control that with skipLibCheck.
  • Prefer @ts-expect-error over @ts-ignore: when the error is fixed, the stale comment turns into a reminder.