// @ts-check in JS; JSDoc to get types
Enable type checking in .js with // @ts-check and JSDoc; add @param/@returns, @type/@typedef, and @template for generic patterns; configure tsconfig for gradual migration.
Intro
Goal: Get TypeScript benefits inside .js files. You will turn on // @ts-check, add JSDoc types, and configure tsconfig for gradual adoption.
- Per-file vs project-wide checking
- @param, @returns, @type, @typedef
- @template for generic-like helpers
Enable checking
Add // @ts-check at the top of a .js file. Use @param/@returns to document and type the function.
// calculator.js
// @ts-check
/**
* Adds two numbers.
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function add(a, b) {
return a + b
}
// Usage: type errors appear if you pass non-numbers
// add("2", 3) // TS will flag this in JS