Sub topic 6
Configuring the project with `tsconfig.json`
This chapter assumes you know JavaScript functions, objects, and modules. You also need basic terminal skills, Node.js, npm, and a package.json file. You will configure the TypeScript compiler for a small Node.js project.
Why use a configuration file?
You can pass options directly to tsc:
npx tsc src/index.ts --outDir distThis works for a quick experiment. It becomes difficult to maintain when a project has many source files and compiler options. A tsconfig.json file records those choices in the project itself.
Create a starter configuration with:
npx tsc --initThe command creates tsconfig.json with many commented options. You can replace its contents with a smaller configuration that matches your project.
A practical Node.js configuration
Create a src directory and place your TypeScript files there. Use this configuration at the project root:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"skipLibCheck": true
},
"include": ["src"]
}The compilerOptions object controls how TypeScript checks and emits files. The include property tells TypeScript which source files belong to this project.
| Option | Purpose |
|---|---|
| `target` | Chooses the JavaScript version that TypeScript emits |
| `module` | Defines how JavaScript modules are interpreted |
| `strict` | Enables strict type-checking rules |
| `rootDir` | Identifies the source directory |
| `outDir` | Identifies the generated-file directory |
| `sourceMap` | Creates files that help debuggers map JavaScript to TypeScript |
| `skipLibCheck` | Skips type-checking of declaration files from dependencies |
With rootDir set to src and outDir set to dist, a file at src/index.ts becomes dist/index.js. TypeScript keeps the relative directory structure below src.
How the configuration controls a build
Run the compiler from the directory containing tsconfig.json:
npx tscTypeScript reads the configuration, finds files under src, checks them, and writes JavaScript to dist.
The process looks like this:
If TypeScript reports an error, it may still emit JavaScript unless you change the emit policy. For development, seeing the error quickly is useful. For a release build, you may want to prevent output when errors exist.
Choosing the important options
`target` and `module`
target selects the JavaScript language level. A modern Node.js version can run ES2022 syntax directly, so "ES2022" avoids unnecessary transformations.
module controls module behavior. "NodeNext" follows Node.js's modern module rules. Your package.json, file extensions, and package configuration must agree with that choice. If your project uses a bundler, the bundler's recommended module setting may differ.
`strict`
"strict": true enables a group of checks designed to expose unsafe assumptions. It includes strictNullChecks, which makes undefined and null distinct from ordinary values.
function firstLetter(text: string | undefined): string {
if (text === undefined) {
return "";
}
return text.charAt(0);
}Without the check, calling charAt on an undefined value could fail at runtime. Strict checking makes the missing case visible during development.
`rootDir`, `outDir`, and `include`
These options separate authored files from generated files:
- Put TypeScript source in
src. - Tell TypeScript to read that directory with
include. - Tell TypeScript where source files begin with
rootDir. - Tell TypeScript where generated files belong with
outDir. - Keep
distout ofincludeso generated files are not treated as new source files.
Exercise
Exercise
In the configuration above, where will TypeScript write the JavaScript for src/utils/math.ts?
Show answer
It will write it to dist/utils/math.js, preserving the path below rootDir.