// @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.
// @ts-check in JS; JSDoc to get types is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 JSTypedef & type
Use @typedef to define a reusable shape and @type to annotate variables. Optional fields are marked with ?.
// models.js
// @ts-check
/** @typedef {{ id: string; name: string; age?: number }} User */
/** @type {User} */
export const currentUser = { id: "u1", name: "Ada" }
/**
* Greets a user.
* @param {User} u
* @returns {string}
*/
export function greet(u) {
return `Hello, ${u.name}!`
}Template params
@template introduces type parameters for generic-like functions in JS. Inference works at call sites.
// helpers.js
// @ts-check
/**
* Maps items with a function, preserving types.
* @template T,U
* @param {T[]} items
* @param {(x: T) => U} f
* @returns {U[]}
*/
export function map(items, f) {
const out = []
for (const x of items) out.push(f(x))
return out
}
// Inference: T=number, U=string
// map([1,2], n => n.toFixed(2))tsconfig for JS
Project-wide: turn on checkJs for all .js files. Set allowJs to include JS files in the program. Use noEmit when a bundler handles output.
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"checkJs": true,
"allowJs": true,
"strict": true,
"noEmit": true
},
"include": ["./**/*.js"],
"exclude": ["node_modules"]
}Migration tips
Migration tips:
- Start with critical files using
// @ts-check. - Create small
@typedefs for shared shapes. - Gradually rename hot paths to
.tsonce types are stable.
ts-check check
Quick check: How do you enable TS checking in a .js file?
Recap
Recap: Turn on // @ts-check, add @param/@returns/@typedef/@template, and tighten tsconfig for JS projects before migrating to .ts.
Frequently asked questions
Is the “// @ts-check in JS; JSDoc to get types” lesson free?
Yes — the full text of “// @ts-check in JS; JSDoc to get types” is free to read here on the web, and the TypeScript Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “// @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. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.
How long does the “// @ts-check in JS; JSDoc to get types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.