Sub topic 7
The Compiler Options You Need First
This chapter assumes you know JavaScript functions, objects, and modules. You should also be comfortable using a terminal and npm, and you should have Node.js installed.
The TypeScript compiler, tsc, uses compiler options to decide three things:
- Which source files belong to the project.
- How to check those files.
- What JavaScript files to write.
You usually store these settings in tsconfig.json at the project root.
A useful starter configuration
A small Node.js project might use this configuration:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"skipLibCheck": true
},
"include": ["src"]
}The compilerOptions object controls compiler behavior. The include property tells TypeScript which files to consider.
The project might have this layout:
ts-demo/
├── src/
│ └── index.ts
├── dist/
├── package.json
└── tsconfig.jsonRun the compiler from the directory containing tsconfig.json:
npx tscTypeScript reads the configuration, checks files under src, and writes JavaScript under dist.
The core options
| Option | What it controls | Common choice |
|---|---|---|
| `target` | JavaScript language version to emit | `ES2022` |
| `module` | Module system used by the output | `NodeNext` for modern Node.js |
| `rootDir` | Common source directory | `src` |
| `outDir` | Directory for generated files | `dist` |
| `include` | Files included in the project | `["src"]` |
| `strict` | Type-checking safety rules | `true` |
`target`
target determines which JavaScript features TypeScript may leave in the output. With "ES2022", the output can use modern JavaScript features supported by current Node.js versions.
A lower target, such as "ES2019", asks TypeScript to transform more newer syntax for older runtimes. TypeScript does not add runtime features that JavaScript lacks. For example, targeting an older version does not automatically add a Promise implementation.
`module`
module controls how imports and exports appear in the emitted JavaScript.
"NodeNext" follows Node.js's modern module rules. It is a good choice when your project uses current Node.js conventions, but your package.json may also need a "type" field to identify whether files use ES modules or CommonJS.
Choose a module setting that matches the runtime or bundler that will execute the output. A mismatch can produce errors even when type checking succeeds.
`rootDir` and `outDir`
rootDir identifies the source tree. outDir identifies the generated tree.
For example, src/utils/math.ts becomes dist/utils/math.js. TypeScript preserves the path below rootDir inside outDir.
rootDir does not decide which files are compiled. include and related settings do that.
`include`
include accepts file patterns. ["src"] includes supported TypeScript files beneath the src directory. You can use more specific patterns when a project has several source areas.
Keep generated folders such as dist outside the included source tree. If necessary, use exclude to prevent TypeScript from considering a directory.
How the settings work together
strict: true enables a group of strict checking rules. It catches problems such as using a value that might be undefined. This option is discussed in more detail in the next chapter, but enabling it in a new project is usually the safest starting point.
sourceMap: true creates map files that help debuggers connect generated JavaScript back to TypeScript. skipLibCheck: true skips checking declaration files from dependencies, which can make builds faster without disabling checks in your own source.
Exercise
In this configuration, where will TypeScript write the JavaScript for src/api/client.ts, and which option determines whether that file is included?
Show answer
It will write the file to dist/api/client.js. The include option selects the source file, while rootDir and outDir determine the corresponding output path.