Skip to content

Commit c566316

Browse files
committed
feat: tsconfig about file inclision keywords
1 parent 3bf6171 commit c566316

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [1.0.0](https://github.com/Rain120/typescript-guide/compare/0.0.1...1.0.0) (2021-04-09)
1+
# [1.0.0](https://github.com/Rain120/typescript-guide/compare/0.0.1...1.0.0) (2021-04-24)
22

33

44
### Bug Fixes
@@ -11,6 +11,7 @@
1111

1212
* blog tips & pdfs & books ([310765f](https://github.com/Rain120/typescript-guide/commit/310765ffd4e95bcde3dc3a33c3ca7115f80d30f1))
1313
* faqs about tsconfig module target ([d40988c](https://github.com/Rain120/typescript-guide/commit/d40988c26b6c287dc72aac94a4c76b2f07cfa8cf))
14+
* faqs add interface vs type ([3bf6171](https://github.com/Rain120/typescript-guide/commit/3bf61710c8395d8ba73adfc0ae4eb7a3be0f5021))
1415
* finished declaration ([b30964d](https://github.com/Rain120/typescript-guide/commit/b30964d056cd2c03bcaa0dd3a19a60215510ec90))
1516
* tsconfig - references ([ad24a19](https://github.com/Rain120/typescript-guide/commit/ad24a197efd8703ab818458246d141b3adf5f226))
1617
* tsconfig module target; remove faqs module, target ([48d3e0f](https://github.com/Rain120/typescript-guide/commit/48d3e0f16f0957b08da24352ca73eb1e5770d382))

docs/.vuepress/utils/alias.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"implements": "implements",
3030
"infer": "infer",
3131
"blog": "博客",
32+
"file-inclusion": "Typescript 文件包含相关配置",
3233
"references": "tsconfig - references",
3334
"tsconfig-module-target": "Typescript Module Target",
3435
"interface-vs-type": "interface 和 type 有什么异同点?",

docs/zh/compile-config/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102

103103
- [tsconfig-module-target](./tsconfig-module-target/README.md)
104104

105+
- [TypeScript 文件包含相关配置](./file-inclusion/README.md)
106+
105107
## 参考资料
106108

107109
[深入理解 TypeScript](https://jkchao.github.io/typescript-book-chinese/)
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
## 定义
2+
3+
这些设置用于确保 `TypeScript` 处理正确的文件。
4+
5+
## 关键字
6+
7+
### extends
8+
9+
默认值: `false`
10+
11+
`extends` 的值是一个字符串,它包含要继承的另一个配置文件的路径。路径可以使用 `Node.js` 样式解析。
12+
13+
首先加载基本文件中的配置,然后由 **继承的配置文件** 中的配置覆盖。**在配置文件中找到的所有相对路径都将相对于它们最初所在的配置文件进行解析。**
14+
15+
值得注意的是,从 **继承的配置文件**[包含(include)](#include)[排除(exclude)](#exclude) 的文件会覆盖从 **基本配置文件**[包含(include)](#include)[排除(exclude)](#exclude) 的文件,并且配置文件之间不允许循环。
16+
17+
目前,唯一被排除在继承之外的顶级属性是 [references](#references)
18+
19+
**Note👇👇👇**
20+
21+
- 继承的配置文件: `tsconfig.json`
22+
23+
- 基本配置文件: `tsconfig.base.json`
24+
25+
`tsconfig.base.json`
26+
```json
27+
{
28+
"compilerOptions": {
29+
"noImplicitAny": true,
30+
"strictNullChecks": true
31+
}
32+
}
33+
```
34+
35+
`tsconfig.json`
36+
```json
37+
{
38+
"extends": "./tsconfig.base"
39+
}
40+
```
41+
42+
在配置文件中找到的具有相对路径的属性(没有从继承中排除)将相对于它们的起始配置文件进行解析。
43+
44+
### exclude
45+
46+
默认值: `["node_modules", "bower_components", "jspm_packages"]`, 如果指定了一个, 则加上 `outDir` 的值。
47+
48+
它是用来指定解析 `include` 时应跳过的文件名或模式数组, 也就是文件过滤列表 (`glob` 匹配模式)。
49+
50+
```json
51+
{
52+
"exclude": ["node_modules"]
53+
}
54+
```
55+
56+
### include
57+
58+
默认值: 除非被指定了文件, 否则就是 `["**/*"]`
59+
60+
指定要包含在程序中的文件名或模式的数组。这些文件名是相对于包含`tsconfig.json` 的目录解析的文件。
61+
62+
```json
63+
{
64+
"include": ["src/**/*", "tests/**/*"]
65+
}
66+
```
67+
68+
它将会包含
69+
70+
```sh
71+
.
72+
├── scripts ⨯
73+
│ ├── lint.ts ⨯
74+
│ ├── update_deps.ts ⨯
75+
│ └── utils.ts ⨯
76+
├── src ✓
77+
│ ├── client ✓
78+
│ │ ├── index.ts ✓
79+
│ │ └── utils.ts ✓
80+
│ ├── server ✓
81+
│ │ └── index.ts ✓
82+
├── tests ✓
83+
│ ├── app.test.ts ✓
84+
│ ├── utils.ts ✓
85+
│ └── tests.d.ts ✓
86+
├── package.json
87+
├── tsconfig.json
88+
└── yarn.lock
89+
```
90+
91+
::: tip
92+
`include``exclude`支持通配符来生成 `glob`模式, 如下 👇👇👇:
93+
94+
- **\*** 匹配 0 或多个字符(不包括目录分隔符)
95+
96+
- **?** 匹配一个任意字符(不包括目录分隔符)
97+
98+
- **\*\*/** 递归匹配任意子目录
99+
100+
如果一个 `glob` 模式里的某部分只包含 `* 或 .*`, 那么仅有支持的文件扩展名类型被包含在内(比如默认 `.ts`, `.tsx`, 和 `.d.ts`, 如果 `allowJs: true` 还包含 `.js``.jsx`)。
101+
:::
102+
103+
### files
104+
105+
默认值: `false`
106+
107+
指定程序中包含的文件的允许列表。如果无法找到任何文件,就会发生错误。
108+
109+
```json
110+
{
111+
"files": ["main.ts", "supplemental.ts"]
112+
}
113+
```
114+
115+
**Note:** 当您只有少量文件且不需要使用 `glob` 来引用许多文件时,这很有用。如果需要,那么使用[include](#include)
116+
117+
### references
118+
119+
默认值: `false`
120+
121+
项目引用是一种将你的 `TypeScript` 程序结构成更小块的方法。使用`Project References` 可以极大地改进构建和编辑器的交互时间, 强制组件之间的逻辑分离, 并以新的和改进的方式组织代码。
122+
123+
```json
124+
{
125+
"files": ["main.ts", "supplemental.ts"]
126+
}
127+
```
128+
129+
你可以在 **官方手册** 中的[项目参考(Project References)](https://www.typescriptlang.org/docs/handbook/project-references.html)资料部分阅读更多关于参考资料如何工作的内容, 或者参考我另一篇笔记获取更多有关于 [Project References](../references/README.md) 的信息。
130+
131+
### typeAcquisition
132+
133+
默认是: `false`
134+
135+
当你的编辑器中有一个 `JavaScript` 项目时, `TypeScript` 会使用`@types` 定义的 `DefinitelyTyped` 集合自动为你的节点模块提供类型。这称为自动类型获取, 您可以使用配置中的 `typeAcquisition` 对象对其进行自定义。
136+
137+
如果您想禁用或自定义此特性, 请创建 `jsconfig.json` 在项目的根目录中
138+
139+
```json
140+
{
141+
"typeAcquisition": {
142+
"enable": false
143+
}
144+
}
145+
```
146+
147+
如果你有一个特定的模块, 应该包括(但不在 `node_modules` 中)
148+
149+
```json
150+
{
151+
"typeAcquisition": {
152+
"include": ["jest"]
153+
}
154+
}
155+
```
156+
157+
如果不应该自动获取某个模块, 例如, 如果该库在您的 `node_modules` 中可用, 但您的团队已经同意不使用它
158+
159+
```json
160+
{
161+
"typeAcquisition": {
162+
"exclude": ["jquery"]
163+
}
164+
}
165+
```
166+
167+
`TypeScript 4.1`中, 还添加了禁用文件名触发类型获取的特殊大小写的功能。
168+
169+
```json
170+
{
171+
"typeAcquisition": {
172+
"disableFilenameBasedTypeAcquisition": true
173+
}
174+
}
175+
```
176+
177+
这意味着在你的项目中有一个像 `jquery.js` 这样的文件不会自动从`DefinitelyTyped` 下载 `JQuery` 的类型。
178+
179+
## 快来耍耍啊
180+
181+
### 🌰🌰
182+
183+
<!-- 题目 -->
184+
185+
```
186+
187+
// template
188+
189+
```
190+
191+
### 游乐场
192+
193+
<br />
194+
195+
<Editor
196+
value='// enjoy yourself'
197+
/>
198+
199+
### 参考答案
200+
201+
```ts
202+
// answer
203+
```
204+
205+
## 参考资料
206+
207+
[typescript lang: File Inclusion](https://www.typescriptlang.org/tsconfig#Project_Files_0)

0 commit comments

Comments
 (0)