"include" and "exclude"

Which files the compiler takes into account, and which it skips.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "strict": true
  },
  "include": ["src", "types/**/*.d.ts"],
  "exclude": ["node_modules", "dist", "**/*.spec.ts"]
}

Definition and usage

#

The include and exclude fields sit at the top level of tsconfig.json, next to compilerOptions. include takes a list of file patterns, for example ["src"] or ["src/**/*.ts"], and exclude removes some of them.

By default, without include, the compiler takes all TypeScript files in the project folder, and exclude by default skips node_modules and the outDir folder.

The exclude field doesn’t block a file if another file in the project imports it. It only removes starting points of the search.

Syntax

#
Syntax
"include": ["src"],
"exclude": ["node_modules", "dist"]

Good practices

#
  • Limit include to the folders with code. The compiler runs faster, and the editor doesn’t analyze unnecessary files.
  • Add your own type declaration files (*.d.ts) to include, otherwise the compiler won’t see them.

Related entries

#

Spotted a mistake or missing an example? Write to us.