Sub topic 2
Install TypeScript and create tsconfig.json
This chapter assumes you know modern JavaScript, can run Node.js and npm commands, and understand that static types describe values before the program runs. You will configure a project where .js and .ts files can coexist during migration.
Install the compiler
TypeScript is a compiler and type checker. The compiler reads TypeScript and JavaScript source files, reports type errors, and can produce JavaScript for Node.js to run.
Install TypeScript and the Node.js type declarations as development dependencies:
npm install --save-dev typescript @types/node
npx tsc --initA development dependency is a package needed to build or check your project, but not normally needed by the deployed program. The @types/node package describes Node.js APIs such as process and fs.
The tsc command is the TypeScript compiler. The --init option creates a starter tsconfig.json file.
Configure the project
Replace the generated configuration with a small migration-friendly setup:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"strict": false
},
"include": ["src"]
}compilerOptions contains compiler settings.
targetselects the JavaScript version emitted by the compiler.moduleselects the module format.commonjsworks with many existing Node.js projects.rootDiridentifies the source directory.outDirplaces compiled files indist.allowJspermits JavaScript files inside the TypeScript project.esModuleInteropmakes many CommonJS and ES module imports work together.skipLibCheckskips checking declaration files from dependencies. This usually reduces unrelated errors.strictenables a group of demanding checks. Keep itfalsewhile establishing the migration, then enable stricter checks in stages.includelimits compilation to files undersrc.
With allowJs, the compiler can process both src/legacy.js and src/new-file.ts. This lets you migrate one file at a time instead of stopping development for a complete rewrite.
Check the configuration
Create a simple source file:
// src/legacy.js
const message = "Migration started";
console.log(message);Ask TypeScript to check and emit the project:
npx tsc
node dist/legacy.jsThe compiler should create dist/legacy.js, and Node.js should print Migration started. TypeScript does not make Node.js run .ts files directly. It first emits JavaScript, unless another runtime tool handles TypeScript.
The migration path now looks like this:
Choose settings deliberately
| Setting | Migration purpose |
|---|---|
| `allowJs` | Lets `.js` and `.ts` files coexist |
| `checkJs` | Enables type checking for JavaScript |
| `strict` | Enables the strongest built-in checks |
Exercise
Why does a gradual migration usually set allowJs to true and strict to false at first?
Show answer
allowJs keeps existing JavaScript files in the project, while strict: false prevents every existing untyped pattern from blocking the first migration steps. Stricter checks can be enabled as files are converted.