checkJs Option
In this page:
Core Concept
The checkJs compiler option extends TypeScript's type checking to plain .js files (not just .ts), inferring types from JSDoc comments and usage patterns rather than explicit type annotations.
Example: Core Concept
/**
* @param {number} x
* @returns {number}
*/
function double(x) {
return x * 2;
}
console.log(double(5));
Basic Setup
Enable it with "checkJs": true alongside "allowJs": true in tsconfig.json, or on a single file with a // @ts-check comment at the top without touching the global config at all.
Example: Basic Setup
// @ts-check
/**
* @param {string} name
*/
function greet(name) {
console.log("Hello, " + name);
}
greet("Ravi");
Typed Example
A typed example: a .js file with /** @param {number} x */ function double(x) { return x * 2; } gets full parameter-type checking at call sites, even though the file has no .ts extension.
Example: Typed Example
// @ts-check
/**
* @param {number} x
* @returns {number}
*/
function double(x) {
return x * 2;
}
console.log(double(5)); // call sites get full parameter-type checking
Project Usage
In a real project, checkJs is useful for catching bugs in legacy .js files that aren't worth a full rewrite yet, using lightweight JSDoc annotations instead of committing to a full TypeScript conversion.
Example: Project Usage
// @ts-check
/**
* @typedef {{ name: string, age: number }} User
*/
/** @param {User} user */
function describe(user) {
return `${user.name} is ${user.age}`;
}
console.log(describe({ name: "Ravi", age: 25 }));
Best Practices
Use // @ts-nocheck at the top of specific noisy legacy files to opt them out individually, rather than disabling checkJs project-wide just because a handful of files generate too many initial errors.
Example: Best Practices
// @ts-nocheck
// Opt a specific noisy legacy file out of checking, instead of disabling
// checkJs project-wide.
console.log("Use @ts-nocheck per-file rather than a global opt-out");
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: