Sub topic 16
Seeing the Configuration the Compiler Really Uses
This chapter assumes you can read a basic tsconfig.json, know what include, exclude, extends and strict do, and can run npx tsc from a terminal. The goal is to stop guessing which settings and files the compiler uses, and to read that from the compiler itself.
Why the file you see is not the whole story
A tsconfig.json is rarely the full configuration. The compiler builds the effective configuration from several sources:
- The file it finds. Without
-p,tsclooks fortsconfig.jsonin the current directory, then in each parent directory. - Any files named in
extends, merged in order. Settings in the inheriting file win. - Command-line flags, which override the file.
- Defaults. Many options depend on others, so
strictturns on a group of flags, andtargetchanges the default forlibanduseDefineForClassFields.
A bug in any of these layers looks the same: a setting you think is on is off, or a file you expect is missing. Two commands answer most of these questions.
npx tsc --init
npx tsc --noEmit
npx tsc --showConfig
npx tsc --listFiles--initwrites a startertsconfig.jsonwith comments. It refuses to overwrite an existing file.--noEmittype-checks and writes no output. It is the usual check for CI and for "does this compile?"--showConfigprints the resolved configuration as JSON and stops.--listFilesprints every file in the compilation, including declaration files fromnode_modules.
Reading `--showConfig`
--showConfig prints the result after extends has been merged and paths have been resolved. Suppose a package uses the shared base file from the previous chapter.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}Running npx tsc --showConfig in that package prints something like this. The exact output depends on your TypeScript version and on the contents of your base file.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": [
"src"
]
}Notice three things:
- The output merges both files into one. You do not have to open the base file to know what applies.
- Paths such as
outDirare shown relative to the config file that is being printed. This confirms where output goes. - The output lists what is set, not every flag's final value.
strict: trueis shown, but the individual flags it enables (noImplicitAny,strictNullChecksand so on) are not expanded. To know whether one of them is on, checkstrictand then look for an explicit override.
That last point matters in migrations. If the output shows "strict": true and "noImplicitAny": false, the second setting wins for that one flag. Order inside the JSON does not matter. An explicit individual flag always beats the umbrella.
{
"compilerOptions": {
"strict": true,
"noImplicitAny": false
}
}Reading `--listFiles`
--listFiles answers "which files are being compiled?" The output is long, because it includes the TypeScript library files and every .d.ts file under node_modules/@types. Filter it to the files you own.
npx tsc --listFiles --noEmit | grep -v node_modulesUse this when:
- A file is missing from the build. Check that it matches
include, is not matched byexclude, and has an extension TypeScript handles. - A file is present that you did not expect. Something imports it, or
includeis wider than you thought. Remember thatexcludeonly filters the initialincludeset. A file that is imported from an included file is still compiled even ifexcludelists it. - Compilation is slow. A huge file count often means a missing
excludeor a strayincludesuch as"**/*"that sweeps in build output.
If you need to know why a file is included, use --explainFiles. It prints each file with the reason, such as "Matched by include pattern" or "Imported via './util' from file 'src/index.ts'".
npx tsc --explainFiles --noEmit | grep -v node_modulesA debugging workflow
When the compiler behaves differently from what you expect, work through the layers in order.
Because the diagram syntax in this chapter must follow a fixed form, the workflow is shown below as a diagram block.
Common errors
| Symptom | Likely cause | How to confirm |
|---|---|---|
| Strict errors do not appear | `strict` is missing or overridden to `false` for one flag | `--showConfig`, then look for explicit overrides |
| Editor and command line disagree | The editor uses a different `tsconfig.json`, often the nearest one to the open file | Run `tsc -p` on the same file's config |
| A file is not type-checked | It is not matched by `include`, or `files` is set and lists only some files | `--listFiles` |
| Output appears in the wrong folder | `outDir` or `rootDir` is resolved relative to a different file than you thought | `--showConfig` shows resolved paths |
| Errors from a root-level script | The root `tsconfig.json` has no `include`, so it picks up every `.ts` file below it | `--listFiles` |
Two cases deserve more detail.
Editor versus terminal. In a repository with several tsconfig.json files, an editor picks the nearest one for each open file. Running npx tsc at the repository root uses only the root file. If the two give different errors, run tsc -p path/to/tsconfig.json --showConfig for the package that owns the file in question.
Test files. A common layout has a tsconfig.json that excludes *.test.ts for the build, plus a second tsconfig.test.json for tests. Running tsc on the first will not check tests. Run --listFiles against each config to see the difference.
Using `-p` to target a config
-p (or --project) points the compiler at a specific file or at a directory that contains one.
npx tsc -p packages/api/tsconfig.json --showConfig
npx tsc -p packages/api --noEmitPassing source files on the command line, as in tsc src/index.ts, ignores tsconfig.json entirely. This surprises people who then see different defaults. If you want the config used, run tsc with no file arguments, or with -p.
Exercises
Exercise
Your tsconfig.json has "strict": true, yet code that passes null to a string parameter compiles without error. Which commands do you run, and what do you look for?
Show answer
Run npx tsc --showConfig and check for "strictNullChecks": false (an explicit override beats strict). Also confirm the right file is used, since the editor and tsc may pick different configs. Run npx tsc --listFiles --noEmit | grep -v node_modules to confirm the file is in the compilation, and use --explainFiles if it is missing.
Exercise
A file listed in exclude still appears in --listFiles. Why can this happen, and which flag shows the reason?
Show answer
exclude only filters the files matched by include. If an included file imports the excluded file, the compiler still loads it. npx tsc --explainFiles prints the reason, for example "Imported via './util' from file 'src/index.ts'".