include
指定需要编译处理的文件列表,支持 glob 模式匹配,文件的解析路径相对于当前项目的tsconfig.json
文件位置
这里要注意,如果指定了files
选项值,则includes
的默认值为[]
,否则默认包含当前项目中所有文件["**/*"]
例如在下面配置中:
{
"include": ["src/**/*", "tests/**/*"]
}
将会包含src
和test
目录中的所有文件,而不会包含scripts
目录中的文件
.
├── scripts ⨯
│ ├── lint.ts ⨯
│ ├── update_deps.ts ⨯
│ └── utils.ts ⨯
├── src ✓
│ ├── client ✓
│ │ ├── index.ts ✓
│ │ └── utils.ts ✓
│ ├── server ✓
│ │ └── index.ts ✓
├── tests ✓
│ ├── app.test.ts ✓
│ ├── utils.ts ✓
│ └── tests.d.ts ✓
├── package.json
├── tsconfig.json
└── yarn.lock
include
和exclude
都支持使用通配符:
*
匹配零个或者多个字符(包括目录的分隔符)?
匹配任一字符(包括目录分隔符)**/
匹配任何层级的嵌套目录
在使用 glob 模式匹配时,如果没有明确指定文件扩展名。那么默认情况下仅会包含.ts
、.tsx
和.d.ts
类型的文件。但是如果allowJs
选项被设置成了true
,那么.js
和.jsx
文件也会被包含其中
exclude
用于指定当解析include
选项时,需要忽略的文件列表
首先要注意,exclude
的默认值是["node_modules", "bower_components", "jspm_packages"]
加上outDir
选项指定的值。因此下面的示例中,虽然没有明确指定exclude
选项值,但它的的值依然为["node_modules", "bower_components", "jspm_packages", "dist"]
{
"include": [],
"outDir": "dist"
}
其次要注意的是,exclude
只会对include
的解析结果有影响。而且,include
的默认值为["**/*"]
,即全部文件
而且,即使在exclude
中指定的被忽略文件,还是可以通过import
操作符、types
操作符、///<reference
操作符以及在files
选项中添加配置的方式对这些被忽略的代码文件进行引用的
files
用来指定需要编译的文件列表(注意,只能是文件,不能是文件夹)
{
"compilerOptions": {},
"files": [
"core.ts",
"sys.ts",
"types.ts",
"scanner.ts",
"parser.ts",
"utilities.ts",
"binder.ts",
"checker.ts",
"tsc.ts"
]
}
而且如果其中任意一个文件无法找到,都会抛出错误
tsconfig.json:10:5 - error TS5023: Unknown compiler option 'files'.
10 "files": ["a.ts", "c.ts"]
~~~~~~~
这个配置项适用于你想要指定的文件数量比较少,并且不需要使用 glob 模式匹配的情况。否则,请使用 include 配置项