Migrating JavaScript to TypeScript
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Enable allowJs and checkJs and read the errors.
- Rename files incrementally and add types at boundaries.
- Keep the build green during the migration.
Overview
#Start without renaming
With allowJs and checkJs, the checker reads .js files too, so you can see errors before converting anything.
{ "compilerOptions": { "allowJs": true, "checkJs": true, "noEmit": true } }
Convert one file at a time
Rename .js to .ts. Start with leaf modules that have few dependencies. Add types at the file's public boundary first, then let inference fill the body.
Add JSDoc or types
For files you are not ready to rename, JSDoc types give partial checking.
/** @param {string} name @returns {number} */
function length(name) { return name.length; }
Keep the build green
Run the bundler and tests after each file. noEmit lets tsc check without disturbing the existing build output.
Common friction
- Implicit
anyfrom untyped dependencies: add a.d.tsor@types. thisin callbacks: annotate or use arrow functions.- Dynamic property access: use an index signature or a Map.
Finish
When every file is .ts and strict is on, remove allowJs and the JSDoc fallbacks.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
With `allowJs` and `checkJs`, are `.js` files type-checked?
Exercise 2Implement
Add a JSDoc type so this function is checked without renaming the file. ```js function add(a, b) { return a + b; } ```
starter.javascriptfunction add(a, b) { return a + b; }Reveal the reference answer
reference.javascript/** @param {number} a @param {number} b @returns {number} */ function add(a, b) { return a + b; }Authored expected output: Compile-time: add is checked as (a: number, b: number) => number.
JSDoc annotations give JavaScript files partial type information.
Exercise 3Explain
Describe a safe incremental migration order and how you keep the build green.
Reveal the reference answer
reference.typescriptTurn on allowJs and checkJs first so the checker sees the existing JavaScript and reports errors without renaming anything. Convert leaf modules with few dependencies before central ones, typing each file's public boundary first. Run the bundler and tests after each file, and use noEmit so tsc checks types without changing the build output. Finish by enabling strict and removing the JavaScript fallbacks.Authored expected output: A written plan naming allowJs/checkJs, leaf-first conversion and noEmit checks.
Small, verifiable steps keep the build green throughout.
Common misconceptions
#- You must convert the whole project at once.
- JSDoc types are ignored by TypeScript.
- allowJs disables type checking entirely.
Follow-up practice
#- typescript-iterators-generators
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.