Advertisement
Intermediate TypeScript, Migrating JavaScript to TypeScript

How does the 'checkJs' option (combined with '// @ts-check') let you type-check plain JavaScript files using JSDoc comments?

Short answer

With checkJs enabled (or a per-file '// @ts-check' comment), TypeScript type-checks .js files using JSDoc-style comments as the source of type information, letting you get real type errors and autocomplete on JavaScript files without converting them to .ts syntax at all.

Save TypeScript mock interview
Advertisement
AModel answer
With checkJs enabled (or a per-file '// @ts-check' comment), TypeScript type-checks .js files using JSDoc-style comments as the source of type information, letting you get real type errors and autocomplete on JavaScript files without converting them to .ts syntax at all.
// @ts-check
/**
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) {
  return a + b;
}
add('1', 2); // Error caught even in a plain .js file
Real-world example
Adding meaningful type safety to legacy JavaScript files that can't easily be renamed to .ts yet, without a full rewrite.
Likely follow-ups
  • Which JSDoc tags does TypeScript understand for describing more complex types, like unions or generics?
Related topics