Sub topic 3
Checking Too Much: Finding Unintended Files and Type Packages
Assume you can run tsc from the command line and understand tsconfig.json, interfaces, generics, and union types. This chapter focuses on the program: the complete set of source files and declaration files that TypeScript loads for one compilation.
A slow check may come from complex types, but it may also come from checking far more files than you intended. Before changing types, inspect the program's boundaries.
How TypeScript Builds the Program
TypeScript starts with the files named by your configuration. It then follows imports, triple-slash references, and type package rules. A file that enters the program can bring in more files.
The include and exclude options control which files TypeScript discovers by using file patterns. They do not block a file that is imported by an included file.
For example, this configuration starts with files under src:
{
"compilerOptions": {
"noEmit": true
},
"include": ["src"],
"exclude": ["src/generated", "src/**/*.test.ts"]
}If src/app.ts imports src/generated/client.ts, the generated file can still be included. The import makes it part of the program. To remove it, change the import, use a different configuration, or separate the code into projects.
TypeScript can also load global declaration packages from node_modules/@types. These packages describe global names such as process, Buffer, or test-framework APIs. Automatic discovery can load more packages than a project needs.
The types option limits automatic global type packages:
{
"compilerOptions": {
"types": ["node"]
},
"include": ["src"]
}This does not prevent an explicit import from loading a package's declarations. It limits automatic inclusion of global packages.
List Every Included File
Use --listFilesOnly to print the files in the program without checking or emitting JavaScript:
npx tsc --noEmit --listFilesOnlyFor a rough count of application and library files, filter out paths under node_modules:
npx tsc --noEmit --listFilesOnly | grep -v node_modules | wc -lThis command is useful for a baseline, but do not treat the count alone as a diagnosis. One large declaration file and one small source file do not cost the same amount to process.
Look for unexpected categories:
- Test files in a production configuration.
- Generated code that should be compiled separately.
- Examples, demos, or story files.
- Multiple copies of the same dependency.
- Declaration files from an unused environment.
- Source files outside the intended package.
On Windows, save the output and inspect it with an editor or a PowerShell filter instead of relying on grep.
Explain Why Each File Was Included
--explainFiles adds the reason each file belongs to the program:
npx tsc --noEmit --explainFiles > explain.txtThe output can show that a file was included because it matched include, was imported by another file, or was referenced by a type library. Read the lines around suspicious files and identify the path that pulled them in.
For example, an unexpected test declaration package may appear because a test package was automatically discovered. An unexpected source file may appear because a broad import or a project-wide glob reaches it.
Use this process:
- Run
--listFilesOnlyand save the file list. - Run
--explainFilesand save the inclusion reasons. - Find files that do not belong in this compilation.
- Trace each file to its import, pattern, or type package.
- Narrow the configuration or dependency boundary.
- Run the commands again and compare the results.
Distinguish Source Scope from Type Scope
A project can have a correct source boundary but still load unnecessary global types. Conversely, limiting types does not remove imported source files.
Use separate configurations when the same repository has different environments. A browser application, a Node.js tool, and a test suite may need different global declarations. Giving each one its own types list prevents unrelated globals from entering that compilation.
Do not add skipLibCheck merely because the file list is large. That option changes checking of declaration files and belongs to a different diagnosis. First confirm which files and packages are present.
Exercise
An excluded file appears in --listFilesOnly. What should you check first?
Show answer
Check whether an included file imports it. exclude does not prevent imported files from entering the program.