Sub topic 2
Choosing which files are compiled
This chapter assumes you can read a basic tsconfig.json and know that tsc finds it by walking up from the current directory. You should also be comfortable with glob patterns such as src/**/*.ts. Here you will learn how include, exclude, and files decide which source files enter a compilation, and why the result sometimes surprises people.
The program is a set of root files plus their imports
The compiler builds a program. It starts from a set of root files, then follows every import, require, and /// <reference> it finds and adds those files too. Three options decide the roots:
filesis an explicit list of paths. Each one must exist, ortscreports error TS6053 ("File not found").includeis a list of glob patterns that match root files.excludeis a list of glob patterns that remove matches frominclude.
All paths and patterns are resolved relative to the directory of the tsconfig.json that contains them. They are not relative to your current working directory.
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"strict": true
},
"include": ["src"],
"exclude": ["src/**/*.test.ts", "src/legacy"]
}This configuration compiles everything under src except test files and the legacy directory.
Glob patterns
include and exclude support three wildcards.
| Pattern | Matches |
|---|---|
| `*` | Zero or more characters, not including a directory separator |
| `?` | Exactly one character, not including a directory separator |
| `**/` | Any directory, nested to any depth |
A pattern whose last segment has no file extension and no wildcard is treated as a directory. So "src" behaves like "src/**/*". That is why "include": ["src"] works without further decoration.
By default, a pattern selects only supported extensions: .ts, .tsx, and .d.ts. With allowJs enabled, .js and .jsx are also supported. If both app.ts and app.js match, the compiler keeps the .ts file and drops the .js one.
Defaults
If you set none of the three options, include defaults to ["**/*"]. That means every supported file in the config's directory and below.
If you set files but not include, include defaults to an empty list. Only the listed files and their imports are compiled.
exclude defaults to node_modules, bower_components, jspm_packages, and the value of outDir. The outDir entry stops the compiler from reading its own output on the next run.
There is a trap here. As soon as you write your own exclude, you replace this default list. If you write "exclude": ["src/legacy"] and your include reaches into a directory containing node_modules, those files are no longer skipped. Re-add node_modules and your output directory when your patterns could reach them.
How the options combine
The three options are not equal peers. The final root set is the union of files and the matches of include, minus the matches of exclude. The one exception is that exclude never removes a file named in files.
The compiler then adds everything those roots import.
exclude does not mean "never compiled"
The most common misunderstanding is treating exclude as a ban. It only filters the include results. If an included file imports an excluded file, the excluded file still enters the program.
Suppose src/index.ts contains this line.
import { oldHelper } from "./legacy/helper";Even with "exclude": ["src/legacy"], legacy/helper.ts is compiled, type-checked, and emitted, because index.ts reaches it through an import. To keep a file out, you must stop importing it. Alternatively, you can use a separate configuration that does not include the importing file.
Command-line files override the config
If you pass file names to tsc directly, as in npx tsc src/index.ts, the compiler ignores tsconfig.json entirely. This catches people who expect their strict setting to apply. To use the config, run npx tsc with no file arguments, or point to it with -p:
npx tsc -p tsconfig.build.json-p accepts either a path to a config file or a directory that contains tsconfig.json.
A common pattern: separate build and editor configs
Tests need type checking in the editor but usually should not land in dist. A frequent solution uses two files. The main tsconfig.json includes everything, and editors pick it up. A second file extends it and narrows the file set for the build.
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}Save this as tsconfig.build.json. Then build with npx tsc -p tsconfig.build.json. When a config uses extends, its own files, include, and exclude replace those of the base config rather than merging with them. The base config's paths stay relative to the base file.
Common errors
- TS18003, "No inputs were found in config file". Your patterns matched nothing. Check that
includepoints at the right directory relative to the config, and that files have supported extensions. - TS6059, "File is not under rootDir". An included or imported file lives outside
rootDir. Either widenrootDir, or move the file, or stop including it. - Output nested one level too deep, such as
dist/src/index.js. WithoutrootDir, the compiler uses the common root of all input files. One stray file outsidesrcmoves that root up a level. SettingrootDirexplicitly makes this an error instead of a silent change. - Slow type checking. A broad default
includecan sweep in build scripts, generated code, or acoveragedirectory. Narrowincludeto your source directory.
Checking what was selected
Guessing is slower than asking the compiler. Two commands answer most questions.
npx tsc --listFiles
npx tsc --explainFiles--listFiles prints every file in the program, including files from node_modules/typescript and @types. --explainFiles adds the reason each file was included, such as "Matched by include pattern" or "Imported via './legacy/helper' from file 'src/index.ts'". Pipe either one through grep to find a specific path. Later chapters cover --showConfig, which shows the config after extends has been applied.
Exercise
Your tsconfig.json has "include": ["src"] and "exclude": ["src/legacy"]. The file src/index.ts imports "./legacy/helper". Is src/legacy/helper.ts compiled? How would you confirm it, and how would you stop it from being compiled?
Show answer
Yes, it is compiled. exclude only filters the include matches, and the import from src/index.ts pulls the file into the program. Run npx tsc --explainFiles and look for src/legacy/helper.ts. Its reason will read as imported from src/index.ts. To stop it, remove the import, or move the importing code out of the configuration's file set.
Exercise
You write "files": ["src/main.ts"] and no include. Another file, src/util.ts, is not imported by main.ts. Is it compiled? What if you add "include": ["src"]?
Show answer
Without include, it is not compiled. Setting files makes include default to an empty list, so src/main.ts and its imports are the only inputs. With "include": ["src"], src/util.ts is matched and compiled, because the roots are the union of files and the include matches.