← Back to TypeScript Course | Chapter 27: Migration from JavaScript | Lesson 3 of 6

checkJs Option

The checkJs option asks TypeScript to type-check included JavaScript files. It can expose errors before files are converted to TypeScript.

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

typescript
/**
 * @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

typescript
// @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

typescript
// @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

typescript
// @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

typescript
// @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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.