Lesson

TypeScript in JS Projects

10 min reading
Free Course

TypeScript in JavaScript Projects: Zero-Build Checking with JSDoc

You do not need to rename every .js file to .ts to benefit from TypeScript's type checker. Enabling checkJs in tsconfig.json (or adding // @ts-check at the top of a .js file) runs TypeScript directly over JavaScript using standard JSDoc comments.

Teams maintaining plain Node scripts or libraries that want zero compilation step frequently choose TypeScript-checked JSDoc.

Checking .js Files with // @ts-check and JSDoc

Add // @ts-check on line 1 and document parameters or imports with @param, @returns, and @typedef:

// @ts-check

/**
 * @typedef {Object} LineItem
 * @property {string} name
 * @property {number} price
 */

/**
 * Compute sum of item prices.
 * @param {readonly LineItem[]} items
 * @returns {number}
 */
export function sumCart(items) {
  return items.reduce((total, item) => total + item.price, 0);
}

You can even import interfaces from a .ts or .d.ts file inside JSDoc using @type {import("./types").User} without emitting any extra JavaScript.

Project-Wide tsconfig.json Settings

Set "allowJs": true, "checkJs": true, and "noEmit": true to validate your entire JavaScript repository inside VS Code and CI via npx tsc.

Common Mistakes to Avoid

  • Using invalid JSDoc syntax: Writing {Array<string>} works, whereas {string} inside @param items without [] rejects arrays.
  • Forgetting "noEmit": true alongside allowJs: Without noEmit, tsc complains about overwriting input .js files unless outDir is separate.
  • Trying complex mapped types inside JSDoc: When types grow intricate, move type definitions into a .d.ts file and reference them via import("./types") inside JSDoc.

Frequently Asked Questions

How do I ignore a single line error in a // @ts-check file?

Place // @ts-expect-error right above the line with a short comment explaining why—never use // @ts-ignore if ts-expect-error can alert you once the bug is fixed.

Can tsc generate .d.ts declaration files from JSDoc-typed .js files?

Yes. Enable "declaration": true and "emitDeclarationOnly": true so consumers get full TypeScript definitions while your source stays pure JavaScript.

Is JSDoc checking as strong as .ts files?

Almost! TypeScript supports generics (@template T), unions, satisfies (/** @satisfies {Config} */), and type casts (/** @type {HTMLElement} */) in JSDoc.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum