File Inclusion

These settings help you ensure that TypeScript picks up the right files.

Exclude - exclude

Specifies an array of filenames or patterns that should be skipped when resolving include.

Important: exclude only changes which files are included as a result of the include setting. A file specified by exclude can still become part of your codebase due to an import statement in your code, a types inclusion, a /// <reference directive, or being specified in the files list.

It is not a mechanism that prevents a file from being included in the codebase - it simply changes what the include setting finds.

  • Default:

    ["node_modules", "bower_components", "jspm_packages"], plus the value of outDir if one is specified.

  • Related:

    include, files

Extends - extends

The value of extends is a string which contains a path to another configuration file to inherit from. The path may use Node.js style resolution.

The configuration from the base file are loaded first, then overridden by those in the inheriting config file. All relative paths found in the configuration file will be resolved relative to the configuration file they originated in.

It’s worth noting that files, include and exclude from the inheriting config file overwrite those from the base config file, and that circularity between configuration files is not allowed.

Currently, the only top-level property that is excluded from inheritance is references.

File Inclusion - 图1Example

configs/base.json:

tsconfig.json:

  1. {
    "": "./configs/base",
    "": ["main.ts", "supplemental.ts"]
    }

tsconfig.nostrictnull.json:

  1. {
    "": "./tsconfig",
    }
    }

Properties with relative paths found in the configuration file, which aren’t excluded from inheritance, will be resolved relative to the configuration file they originated in.

  • Default:

    false

  • Released:

    2.1

Files - files

Specifies an allowlist of files to include in the program. An error occurs if any of the files can’t be found.

  1. {
    "": [
    "core.ts",
    "sys.ts",
    "types.ts",
    "scanner.ts",
    "parser.ts",
    "utilities.ts",
    "binder.ts",
    "checker.ts",
    "tsc.ts"
    ]
    }

This is useful when you only have a small number of files and don’t need to use a glob to reference many files. If you need that then use include.

Include - include

Specifies an array of filenames or patterns to include in the program. These filenames are resolved relative to the directory containing the tsconfig.json file.

  1. json
    {
    "include": ["src/**/*", "tests/**/*"]
    }

Which would include:

  1. .
  2. ├── scripts
  3. ├── lint.ts
  4. ├── update_deps.ts
  5. └── utils.ts
  6. ├── src
  7. ├── client
  8. ├── index.ts
  9. └── utils.ts
  10. ├── server
  11. └── index.ts
  12. ├── tests
  13. ├── app.test.ts
  14. ├── utils.ts
  15. └── tests.d.ts
  16. ├── package.json
  17. ├── tsconfig.json
  18. └── yarn.lock

include and exclude support wildcard characters to make 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 a glob pattern doesn’t include a file extension, then only files with supported extensions are included (e.g. .ts, .tsx, and .d.ts by default, with .js and .jsx if allowJs is set to true).

  • Default:

    [] if files is specified, otherwise ["**/*"]

  • Related:

    files, exclude

  • Released:

    2.0

References - references

Project references are a way to structure your TypeScript programs into smaller pieces. Using Project References can greatly improve build and editor interaction times, enforce logical separation between components, and organize your code in new and improved ways.

You can read more about how references works in the Project References section of the handbook

  • Default:

    false

Type Acquisition - typeAcquisition

When you have a JavaScript project in your editor, TypeScript will provide types for your node_modules automatically using the DefinitelyTyped set of @types definitions. This is called automatic type acquisition, and you can customize it using the typeAcquisition object in your configuration.

If you would like to disable or customize this feature, create a jsconfig.json in the root of your project:

  1. json
    {
    "typeAcquisition": {
    "enable": false
    }
    }

If you have a specific module which should be included (but isn’t in node_modules):

  1. json
    {
    "typeAcquisition": {
    "include": ["jest"]
    }
    }

If a module should not be automatically acquired, for example if the library is available in your node_modules but your team has agreed to not use it:

  1. json
    {
    "typeAcquisition": {
    "exclude": ["jquery"]
    }
    }

In TypeScript 4.1, we added the ability to disable the special-casing where a filename would trigger type acquisition:

  1. json
    {
    "typeAcquisition": {
    "disableFilenameBasedTypeAcquisition": true
    }
    }

This means that having a file like jquery.js in your project would not automatically download the types for JQuery from DefinitelyTyped.

  • Default:

    false

File Inclusion - 图2Compiler Options

These options make up the bulk of TypeScript’s configuration and it covers how the language should work.