Exclude using a pattern in tsconfig.json in TypeScript

avatar

Borislav Hadzhiev

Sat Mar 12 20222 min read

banner

Photo by Krishh

Exclude using a pattern in tsconfig.json in TypeScript #

Use wildcard characters to exclude using a pattern in tsconfig.json. The excludearray in thetsconfig.json file supports wild card characters to make glob patterns. For example, an asterisk * matches zero or more characters excluding directory separators.

tsconfig.json
{ "compilerOptions": { // ... your compiler options }, "include": ["src/**/*"], "exclude": [ "node_modules", "src/**/*.spec.ts", "src/**/*.test.ts", "src/some-directory" ] }

The example above uses the include option to specify a pattern to include in the codebase.

The filenames or specified pattern is resolved relative to the directory, which contains your tsconfig.json file.

The exclude array contains filenames or patterns that should be skipped when resolving the include array.

The exclude option changes what the include option finds, effectively filtering out some folders or files from compilation.

If you don't want to compile your tests, but want to still have type checking enabled in your test files, check out my other article - Exclude test files from Compilation in TypeScript.

The include and exclude options support wildcard characters for glob patterns:

  • * - matches zero or more characters (excluding directory separators)
  • ? - matches any one character (excluding directory separators)
  • **/ matches any directory nested to any level
If you don't specify the file extension in your glob pattern, then only files with supported extensions are included.

By default files with the following extensions are included: - .ts, .tsx, .d.ts.

If you've set allowJs to true in your tsconfig.json options, then .js and .jsx files are also included by default.

The src/**/*.spec.ts glob pattern matches all files that have a .spec.ts extension in the src directory.

tsconfig.json
{ "compilerOptions": { // ... your compiler options }, "include": ["src/**/*"], "exclude": [ "node_modules", "src/**/*.spec.ts", "src/**/*.test.ts", "src/some-directory" ] }

Regardless of where exactly in the src directory the file with the .spec.ts extension is located, it will be excluded.

Note that we have specified the contents of the whole src directory in the include option, so we are using the exclude array to filter out some directories and files that we don't want to compile.

If you don't explicitly add the exclude array in your tsconfig.json file, it defaults to node_modules, bower_components and jspm_packages.

Adding a pattern or files to your exclude array does not prevent the files from being included in the codebase, it changes what the include setting finds.

For example, if you have the src/some-directory path in your exclude array and create a file under src/some-directory/my-file.ts, it could still be included in your project if you import any of its exports in a file that is being type checked.

Excluded files could also end up being a part of your project if you add them to the files option in your tsconfig.json file.

If you don't want to compile your tests, but want to still have type checking enabled in your test files, check out my other article - Exclude test files from Compilation in TypeScript.

Use the search field on my Home Page to filter through my more than 1,000 articles.