Sub topic 6
Configuring the project with tsconfig.json
This chapter assumes you can run tsc through npx, that you know what a compile step does, and that you can read a package.json. You should also be comfortable with JavaScript modules. Here you learn how tsc finds and reads its configuration file, how that file selects input files, and how to split configuration across several files. The next chapters go through individual compiler options in detail. This one covers the file's structure and behavior.
Why a config file replaces command-line flags
You can compile one file by naming it on the command line:
npx tsc src/index.ts --outDir dist
node dist/index.jsThis works for experiments, but it has two problems. Every option has to be repeated on each run. Also, when you pass files on the command line, tsc ignores tsconfig.json completely. A project whose options live in the config file silently compiles with default settings if someone types a file name.
A tsconfig.json file fixes both problems. It marks the root of a TypeScript project, and it records which files belong to the project and which options apply to them. Your editor reads the same file, so the editor and the compiler agree about what is an error.
Generating a starting file
From the project root, run:
npx tsc --initThis writes a tsconfig.json in the current directory. Most options appear commented out with short descriptions. The exact contents depend on the TypeScript version you installed, because the default template has changed between releases. Read the generated file once, then replace it with a small file you understand. Large configs full of options nobody chose are hard to maintain.
A minimal config for a Node project looks like this:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"skipLibCheck": true
},
"include": ["src"]
}The file has two kinds of settings:
compilerOptionsholds the options that change how the compiler checks and emits code.include,exclude,files,extends, andreferencesare top-level properties. They describe which files make up the project and how it relates to other configs.
Beginners often put include inside compilerOptions. tsc then reports an unknown compiler option, or it ignores your intent. Keep these two groups separate.
The file is parsed more leniently than strict JSON. Comments and trailing commas are allowed. Editors treat it as JSON with comments, so annotating a setting is safe.
How tsc finds the config
When you run tsc with no file arguments, it looks for tsconfig.json in the current directory. If it finds none, it checks the parent directory, and keeps moving upward until it finds one or reaches the filesystem root. If it finds nothing, it prints the help text.
You can pick a config explicitly with -p (long form --project). The argument is either a file or a directory that contains a tsconfig.json:
npx tsc -p tsconfig.build.json
npx tsc -p packages/apiEditors use the same idea per file. For each source file, the editor uses the nearest tsconfig.json that includes it. A file that no config includes is checked in an inferred project with default options. This explains a common surprise: a file shows different errors in the editor than in tsc, because the editor is applying different settings to it.
Choosing the input files
Four properties control which files the compiler reads.
| Property | Meaning |
|---|---|
| `files` | An explicit list of file paths. A missing file is an error. |
| `include` | Glob patterns for files to add. |
| `exclude` | Glob patterns to remove from what `include` matched. |
| `extends` | A base config to inherit from. |
Some details matter in practice.
- Default input set. If neither
filesnorincludeis present, the compiler takes every supported file in the config's directory and its subdirectories. Supported extensions are.ts,.tsx, and.d.ts. Files ending in.jsor.jsxare added only whenallowJsis on. - Default exclusions. If you leave out
exclude, it defaults tonode_modules,bower_components,jspm_packages, and youroutDir. If you write your ownexclude, you replace this default list. Addnode_modulesback if you still want it excluded. - Glob syntax.
*matches any characters within one path segment,?matches one character, and**/matches any number of nested directories. A pattern that names a directory, such as"src", means everything under it. - Imports pull files in.
excludeonly filters the results ofinclude. If an included file imports an excluded file, the compiler still reads and compiles the imported file. To keep a file out of the build entirely, make sure nothing included imports it. - Paths are relative to the config file, not to the directory where you run
tsc. This holds forinclude,exclude,rootDir, andoutDir.
If a config matches no files, tsc stops with error TS18003, "No inputs were found in config file". The usual causes are a misspelled directory in include, or an include that points at a folder with only .js files.
Checking what the compiler actually sees
Two flags remove most of the guesswork:
npx tsc --showConfig
npx tsc --listFilesOnly--showConfig prints the final configuration after extends is resolved and defaults are filled in. --listFilesOnly prints every file that would be part of the compilation and stops without compiling. This list includes files from node_modules that your code imports, plus the built-in library declaration files, so it is long. Filter it to find your own files:
npx tsc --listFilesOnly | grep -v node_modulesIf a file you expected is missing, check include. If a file you did not expect is present, use --explainFiles to see why each file was added.
Where files land: rootDir and outDir
The compiler mirrors the layout of the source tree under outDir. With the config above, src/index.ts becomes dist/index.js, and src/util/math.ts becomes dist/util/math.js.
The mirroring starts at rootDir. If you do not set rootDir, the compiler uses the longest directory path shared by all input files. That default can change without warning. Say your project has only src/index.ts. The output goes to dist/index.js. Add a file scripts/seed.ts to the inputs, and the common root moves up to the project directory. The output then becomes dist/src/index.js, and every path that referred to dist/index.js breaks. Setting rootDir explicitly prevents this.
An explicit rootDir has its own failure mode. If an included file lies outside it, tsc reports error TS6059, "File is not under 'rootDir'". The fix is either to move the file, or to narrow include so it matches only files under rootDir.
Sharing configuration with extends
Larger projects often need more than one config. Typical reasons are that tests should be type-checked but not shipped, or that several packages share the same strictness rules. The extends property lets one config inherit from another.
A base config checks everything and writes nothing:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"skipLibCheck": true,
"noEmit": true
},
"include": ["src", "test"]
}Save this as tsconfig.json. A second file, tsconfig.build.json, inherits from it and changes only what differs:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}Now npx tsc type-checks the whole project, which is what editors and CI use. npx tsc -p tsconfig.build.json emits only the shipping code to dist.
The merge rules are as follows:
- Options in
compilerOptionsare merged. A value in the child replaces the same option in the base. files,include, andexcludein the child replace the base values. They are not merged.- Relative paths inside an inherited file resolve relative to that file, not to the child.
extendscan name a package, such as one of the shared@tsconfig/*presets, as well as a relative path.
To confirm the result, run npx tsc -p tsconfig.build.json --showConfig and read the merged output.
Common errors at a glance
| Symptom | Likely cause | Fix |
|---|---|---|
| TS18003, no inputs found | `include` matches no `.ts` files | Correct the path, or check where the config file sits. |
| TS6059, file not under `rootDir` | An included file is outside `rootDir` | Narrow `include` or move the file. |
| Output appears in `dist/src` | `rootDir` not set and inputs span several folders | Set `rootDir` explicitly. |
| Editor and `tsc` disagree | The editor used a different config, or none | Check that the file is matched by the nearest config's `include`. |
| Test files appear in `dist` | Base config includes tests and is used for the build | Use a separate build config with `exclude`. |
| An excluded file still compiles | An included file imports it | Remove the import, or restructure. |
Exercises
Exercise
You run npx tsc src/index.ts --outDir dist in a project whose tsconfig.json sets "strict": true. Is strict mode applied? What should you run instead?
Show answer
No. When file names are passed on the command line, tsc ignores tsconfig.json, so strict mode is not applied. Run npx tsc with no file names so it reads the config, or use npx tsc -p tsconfig.json.
Exercise
A project has src/index.ts and compiles to dist/index.js. A teammate adds scripts/seed.ts and widens include to cover both folders. Afterward, the output appears at dist/src/index.js. Why did this happen, and how do you prevent it?
Show answer
rootDir was not set, so the compiler used the longest common directory of all inputs. After the new file was added, that directory moved up to the project root, and the src folder became part of the output path. Setting "rootDir": "src" fixes the layout, and scripts/seed.ts then needs to be moved or excluded, or tsc reports TS6059.
Exercise
In tsconfig.build.json, you write "extends": "./tsconfig.json" and "include": ["src"], while the base has "include": ["src", "test"]. Which files does the build config include, and which command shows you the merged result?
Show answer
Only src. A child's include replaces the base value instead of merging with it. Run npx tsc -p tsconfig.build.json --showConfig to see the merged configuration.