在 TS 中使用 tsconfig.json 从编译中排除文件夹

目录

Exclude a Folder from compilation with tsconfig.json in TS

  1. 在 TS 中使用 tsconfig.json 从编译中排除文件夹
  2. 在 TypeScript 中排除使用 tsconfig.json 中的模式

在 TS 中使用 tsconfig.json 从编译中排除一个文件夹

使用文件exclude中的选项tsconfig.json从 TypeScript 的编译中排除文件夹。

exclude选项更改include设置查找的内容并默认为
node_modulesbower_components

tsconfig.json文件
{ "compilerOptions": { // ... your compiler options }, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts", "src/example-nested"] }

该示例使用include
选项指定要包含在代码库中的模式。

文件名或指定模式相对于包含您的tsconfig.json文件的目录进行解析。

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:

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

# Exclude using a pattern in tsconfig.json in TypeScript

The example above uses a pattern to exclude all files that have a .spec.ts
extension and are located in the src directory.

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

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

Note that we 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.

The src/example-nested path points to a subdirectory called example-nested
that’s located in the src directory.

The contents of the src/example-nested directory wouldn’t be type checked or
compiled when you run the tsc command.

So, if I had the following function under src/example-nested/do-math.ts,
TypeScript would just ignore the file and default the types to any.

src/example-nested/do-math.ts
// 👇️ function sum(a: any, b: any): any export function sum(a, b) { return a + b; }
However, if you import this function in a file that is type checked in your codebase, the do-math.ts file will become a part of your project, even though it is in the exclude array.

This makes sense because you don’t want to rely on files that are excluded from
compilation and end up disappearing when you build your project.

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.

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

例如,如果您的数组src/some-directory中有路径exclude并在 下创建了一个文件src/some-directory/my-file.ts,如果您将它的任何导出导入到正在接受类型检查的文件中,它仍可能包含在您的项目中。

如果您将排除的文件添加到文件中的文件选项,
则它们也可能最终成为项目的一部分
tsconfig.json