TypeScript in JS Projects
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 itemswithout[]rejects arrays. - Forgetting
"noEmit": truealongsideallowJs: WithoutnoEmit,tsccomplains about overwriting input.jsfiles unlessoutDiris separate. - Trying complex mapped types inside JSDoc: When types grow intricate, move type definitions into a
.d.tsfile and reference them viaimport("./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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.