Add TypeScript to an Existing JavaScript Project

Install TypeScript, generate a tsconfig.json, and convert files incrementally without breaking your build.

intermediate12 minallUpdated August 2026
#typescript#tsconfig#node.js#migration

Requirements

0/4 steps · 0%
01

Install TypeScript

bash
$ npm install -D typescript @types/node

@types/node provides type definitions for Node's built-in modules (fs, path, etc.) — without it, TypeScript treats them as untyped.

02

Generate a tsconfig.json

bash
$ npx tsc --init

This scaffolds a tsconfig.json with sensible commented-out defaults. The settings you'll touch most:

`strict` — enables the full set of strict type-checks. `target` — which JS version to compile down to. `module` — the module system (esnext for bundlers, commonjs for plain Node). `outDir` — where compiled .js files go.

03

Migrate gradually

You don't need to convert everything at once. Set these two options so .ts and .js files coexist:

tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"strict": false
}
}

Rename one file at a time from .js to .ts, fix the type errors it surfaces, and move on to the next. Tighten strict to true once most of the codebase is converted.

04

Add a typecheck script

package.json
{
"scripts": {
"typecheck": "tsc --noEmit"
}
}
bash
$ npm run typecheck

Success

--noEmit type-checks without writing output files — useful as a CI step or pre-commit check.

Troubleshooting

TypeScript needs type information for every import, and the package either ships none or its `@types` package isn't installed.

bash
$ npm install -D @types/x

If no @types package exists for it, declare a fallback module type in a .d.ts file:

typescript
declare module "x";