Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Projects, Files and Watch Options

TypeScript files, include and exclude options

Reference for the tsconfig files, include and exclude fields: glob wildcards, default values, how imports pull in extra files, and common surprises.

Three top-level tsconfig fields decide which files make up a program. files is an explicit list of paths, nothing more. include is a list of glob patterns that select files. exclude removes files from what include matched. They are siblings of compilerOptions, not part of it.

If you give neither files nor include, the compiler takes every .ts, .tsx and .d.ts file in the tsconfig's folder and below. exclude then defaults to node_modules, bower_components, jspm_packages and the outDir. Anything the selected files import is pulled into the program too, even when it matches exclude.

JSON
{
  "include": ["src/**/*"],
  "exclude": ["src/**/*.test.ts"]
}

Try it

Patterns use * (any characters within one folder name), ** (any number of folders) and ? (one character). This small matcher shows how they select files.

TypeScript A tiny glob matcher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Imports add files beyond include

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{
  "files": ["src/main.ts", "src/types.d.ts"],
  "include": ["src/**/*", "tests/**/*"],
  "exclude": ["**/*.spec.ts", "dist"]
}
Name Description
files An exact list of files. Missing files are an error. No wildcards.
include Glob patterns. A pattern ending in a folder name with no extension means all supported files inside it. Default: **/*.
exclude Glob patterns removed from include's result. Does not affect files or imported files.

Usage notes

  • Supported extensions are .ts, .tsx, .d.ts, and with allowJs also .js and .jsx; .mts, .cts and their outputs follow the same rule.
  • exclude only filters include. To keep a file out of the program, make sure nothing imports it.
  • If files is set and include is not, only the listed files are roots.
  • When using extends, files, include and exclude from the child replace the parent's; paths resolve relative to the config that defines them.
  • List dist in exclude if it is not covered by outDir, otherwise built .d.ts files may be picked up.
  • tsc --listFiles prints what ended up in the program, which is the quickest way to debug a surprise.