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.
{ "compilerOptions": { "allowJs": true, "checkJs": true, "noEmit": true } }
Try it
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.
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
{ "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-checkin a few files, then enablecheckJsand add// @ts-nocheckto files you are not ready for. The list of@ts-nocheckfiles is your migration to-do list. - JSDoc supplies types.
@param {number} n,@returns {string},@type {Map<string, number>},@typedefand@template Tcover most needs; see JSDoc type annotations. - Same strictness switches.
strictand the other type-checking options apply to JavaScript files too, so a strict project reports more in.jsthan a lenient one. - Common false alarms. Properties added to functions or objects after creation, and
module.exportspatterns, may need a JSDoc@typeor a@typedefto describe them. - Ignored libraries. Declaration files and
node_modulesare not type-checked as your code; control that withskipLibCheck. - Prefer
@ts-expect-errorover@ts-ignore: when the error is fixed, the stale comment turns into a reminder.