Course contentsShow
← JavaScript
Lesson 2622 of 2,72353. Type Safety Without TypeScriptFree lesson

Enabling @ts-check in JavaScript Files

Add // @ts-check at the top of a .js file to enable TypeScript's type checking without converting to .ts.

Enabling @ts-check in JavaScript Files

You'll learn how to activate TypeScript's type checker in plain JavaScript files without converting them to TypeScript.

What Is @ts-check?

By placing a single comment—// @ts-check—at the very top of a .js file, you tell TypeScript's compiler to analyze that file for type errors. This leverages all the JSDoc annotations you've been writing (like @type, @param, @typedef) and checks them against actual usage in your code.

Think of it as turning on a spell-checker for a document you've already written. The document doesn't change format (still .js), but now you get real-time feedback on mistakes.

How It Works

// @ts-check

/**
 * @param {string} name
 * @param {number} age
 * @returns {string}
 */
function greet(name, age) {
  return `Hello, ${name}. You are ${age} years old.`;
}

greet("Alice", "30"); // ❌ Error: Argument of type 'string' is not assignable to parameter of type 'number'

With @ts-check enabled, TypeScript will flag the error immediately in your editor—even though this is regular JavaScript. Without it, JSDoc comments are just documentation hints.

When to Use It

Enable @ts-check file-by-file as you add JSDoc annotations. This gradual approach lets you add type safety to legacy codebases without a full migration. It works best in projects where you've already documented types with @typedef, @param, and friends.

You can also configure this globally in a jsconfig.json or tsconfig.json with "checkJs": true, but the inline comment gives you fine-grained control per file.

Key Takeaway: // @ts-check activates TypeScript's type checker in .js files, turning your JSDoc annotations into enforced contracts without leaving JavaScript.