include and exclude: Selecting Files
You'll learn how to tell TypeScript exactly which files to compile and which ones to ignore.
When you run tsc, TypeScript needs to know which files in your project should be type-checked and compiled. By default, it processes every .ts file it can find, which often includes files you don't want compiled—like test files, build scripts, or third-party code.
How include Works
The include property in tsconfig.json lets you specify patterns for which files to include in compilation:
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
This tells TypeScript: "Only compile files inside the src folder and all its subfolders." The **/* pattern means "any file at any depth."
How exclude Works
The exclude property does the opposite—it tells TypeScript which files to skip:
{
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts", "node_modules"]
}
Now TypeScript compiles everything in src, except test files and anything in node_modules.
The Analogy
Think of include as your guest list for a party and exclude as the "do not admit" list. You invite everyone from your neighborhood (include), but specifically exclude certain addresses (exclude).
Default Behavior
If you omit include, TypeScript processes all files in your project. If you omit exclude, it automatically excludes node_modules by default—but adding your own exclude overrides this, so you'll need to re-add node_modules if needed.
Key Takeaway: Use include to define which files TypeScript should compile and exclude to skip specific patterns, keeping your compilation focused and efficient.