Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b8a44da

Browse files
committedJul 24, 2023
docs: finish chapter namespace
1 parent f392f5d commit b8a44da

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed
 

‎chapters.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- generics.md: 泛型
1313
- enum.md: Enum 类型
1414
- assert.md: 类型断言
15+
- module.md: 模块
16+
- namespace.md: namespace
1517
- operator.md: 运算符
1618
- mapping.md: 类型映射
1719
- utility.md: 类型工具

‎docs/decorator.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class A {} // "hi, this is class A {}"
5454

5555
上面的代码就可以顺利通过编译了,代码含义这里先不解释。大家只要理解,类`A`在执行前会先执行装饰器`simpleDecorator()`,并且会向装饰器自动传入参数就可以了。
5656

57-
装饰器有多种形式,基本上只要在`@`符号后面添加表达式都是可以。下面都是合法的装饰器。
57+
装饰器有多种形式,基本上只要在`@`符号后面添加表达式都是可以的。下面都是合法的装饰器。
5858

5959
```typescript
6060
@myFunc
@@ -63,7 +63,7 @@ class A {} // "hi, this is class A {}"
6363
@libraryModule.prop
6464
@someObj.method(123)
6565

66-
@(wrap(dict['prop']))
66+
@(wrap(dict['prop']))
6767
```
6868

6969
注意,`@`后面的表达式,最终执行后得到的应该是一个函数。
@@ -108,17 +108,18 @@ type Decorator = (
108108
name: string | symbol;
109109
addInitializer(initializer: () => void): void;
110110

111-
// Don’t always exist:
111+
// 以下属性只在某些使用场合存在:
112112
static: boolean;
113113
private: boolean;
114114
access: {get: () => unknown, set: (value: unknown) => void};
115115
}
116-
) => void | ReplacementValue; // only fields differ
116+
) => void | ReplacementValue;
117117
```
118118

119-
上面代码中,`Decorator`是装饰器的类型定义。它是一个函数,接受`value``context`两个参数。
119+
上面代码中,`Decorator`是装饰器的类型定义。它是一个函数,使用时会接收到`value``context`两个参数。
120120

121-
其中,`value`参数是所装饰的对象,`context`是装饰器的上下文对象,TypeScript 提供一个原生接口`ClassMethodDecoratorContext`,描述这个对象。
121+
- `value`:所装饰的对象。
122+
- `context`:上下文对象,TypeScript 提供一个原生接口`ClassMethodDecoratorContext`,描述这个对象。
122123

123124
```typescript
124125
function decorator(
@@ -129,6 +130,8 @@ function decorator(
129130
}
130131
```
131132

133+
上面是一个装饰器函数,其中第二个参数`context`的类型就可以写成`ClassMethodDecoratorContext`
134+
132135
`context`对象有以下属性。
133136

134137
(1)`kind`:字符串,表示装饰器类型,可能取以下的值。

‎docs/module.md

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
模块本身就是一个作用域,不属于全局作用域。模块内部的变量、函数、类只在内部可见,对于模块外部是不可见的。暴露给外部的接口,必须用 export 命令声明;如果其他文件要使用模块的接口,必须用 import 命令来输入。
88

9-
如果一个文件不包含 export 语句,但是希望把它当作一个模块(即内部变量对外不可见),可以在文件中添加一行语句
9+
如果一个文件不包含 export 语句,但是希望把它当作一个模块(即内部变量对外不可见),可以在脚本头部添加一行语句
1010

1111
```typescript
1212
export {};
@@ -33,20 +33,12 @@ export { Bool };
3333
假定上面的模块文件为`a.ts`,另一个文件`b.ts`就可以使用 import 语句,输入这个类型。
3434

3535
```typescript
36-
import { Bool } from './a.js';
36+
import { Bool } from './a';
3737

3838
let foo:Bool = true;
3939
```
4040

41-
上面示例中,import 语句加载的是一个类型。注意,它是从文件`a.js`加载,而不是从`a.ts`加载,因为在代码运行环境是 JS 环境,所以要写成从 JS 文件加载,否则报错。
42-
43-
TypeScript 允许加载模块时,省略模块文件的后缀名,它会自动定位。
44-
45-
```typescript
46-
import { Bool } from './a';
47-
```
48-
49-
上面示例中,模块名写成`./a`,TypeScript 会自动定位到`./a.ts`
41+
上面示例中,import 语句加载的是一个类型。注意,加载文件写成`./a`,没有写脚本文件的后缀名。TypeScript 允许加载模块时,省略模块文件的后缀名,它会自动定位,将`./a`定位到`./a.ts`
5042

5143
编译时,可以两个脚本同时编译。
5244

@@ -85,7 +77,7 @@ export interface A {
8577
export let a = 123;
8678

8779
// b.ts
88-
import { A, a } from './a.js';
80+
import { A, a } from './a';
8981
```
9082

9183
上面示例中,文件`a.ts`的 export 语句输出了一个类型`A`和一个正常接口`a`,另一个文件`b.ts`则在同一条语句中输入了类型和正常接口。
@@ -95,19 +87,19 @@ import { A, a } from './a.js';
9587
第一个方法是在 import 语句输入的类型前面加上`type`关键字。
9688

9789
```typescript
98-
import { type A, a } from './a.js';
90+
import { type A, a } from './a';
9991
```
10092

101-
上面示例中,import 语句输入的类型`A`前面有`type`关键字,表示这是一个类型。
93+
上面示例中,import 语句输入的类型`A`前面有`type`关键字,表示这是一个类型。
10294

10395
第二个方法是使用 import type 语句,这个语句只能输入类型,不能输入正常接口。
10496

10597
```typescript
10698
// 正确
107-
import type { A } from './a.js';
99+
import type { A } from './a';
108100

109101
// 报错
110-
import type { a } from './a.js';
102+
import type { a } from './a';
111103
```
112104

113105
上面示例中,import type 输入类型`A`是正确的,但是输入正常接口`a`就会报错。
@@ -153,7 +145,7 @@ export type { Point };
153145
上面示例中,由于使用了 export type 语句,输出的并不是 Point 这个类,而是 Point 代表的实例类型。输入时,只能作为类型输入。
154146

155147
```typescript
156-
import type { Point } from './module.js';
148+
import type { Point } from './module';
157149

158150
const p:Point = { x: 0, y: 0 };
159151
```
@@ -162,7 +154,7 @@ const p:Point = { x: 0, y: 0 };
162154

163155
## importsNotUsedAsValues
164156

165-
输入类型的 import 语句,编译时怎么处理
157+
输入类型的 import 语句,编译时怎么处理呢
166158

167159
TypeScript 提供了`importsNotUsedAsValues`编译设置项,有三个可能的值。
168160

@@ -175,7 +167,7 @@ TypeScript 提供了`importsNotUsedAsValues`编译设置项,有三个可能的
175167
请看示例,下面是一个输入类型的 import 语句。
176168

177169
```typescript
178-
import { TypeA } from './a.js';
170+
import { TypeA } from './a';
179171
```
180172

181173
上面示例中,`TypeA`是一个类型。
@@ -185,7 +177,7 @@ import { TypeA } from './a.js';
185177
`preserve`的编译结果会保留该语句,但会把删掉类型的部分。
186178

187179
```typescript
188-
import './a.js';
180+
import './a';
189181
```
190182

191183
上面示例中,编译后的 import 语句不从`a.js`输入任何接口,但是会引发`a.js`的执行,因此会保留`a.js`里面的副作用。
@@ -202,14 +194,14 @@ CommonJS 是 Node.js 的专用模块格式,与 ES 模块格式不兼容。
202194

203195
### import = 语句
204196

205-
TypeScript 使用 import = 语句输入 CommonJS 模块。
197+
TypeScript 使用`import =`语句输入 CommonJS 模块。
206198

207199
```typescript
208200
import fs = require('fs');
209201
const code = fs.readFileSync('hello.ts', 'utf8');
210202
```
211203

212-
上面示例中,使用 import = 语句和`require()`命令输入了一个 CommonJS 模块。模块本身的用法跟 Node.js 是一样的。
204+
上面示例中,使用`import =`语句和`require()`命令输入了一个 CommonJS 模块。模块本身的用法跟 Node.js 是一样的。
213205

214206
除了使用`import =`语句,TypeScript 还允许使用`import * as [接口名] from "模块文件"`输入 CommonJS 模块。
215207

@@ -221,15 +213,15 @@ import fs = require('fs');
221213

222214
### export = 语句
223215

224-
TypeScript 使用 export = 语句,输出 CommonJS 模块的对象,等同于 CommonJS 的`module.exports`对象。
216+
TypeScript 使用`export =`语句,输出 CommonJS 模块的对象,等同于 CommonJS 的`module.exports`对象。
225217

226218
```typescript
227219
let obj = { foo: 123 };
228220

229221
export = obj;
230222
```
231223

232-
export = 语句输出的对象,只能使用 import = 语句加载。
224+
`export = `语句输出的对象,只能使用`import =`语句加载。
233225

234226
```typescript
235227
import obj = require('./a');
@@ -249,7 +241,7 @@ import { TypeA } from './a';
249241

250242
模块定位有两种方法,一种称为 Classic 方法,另一种称为 Node 方法。可以使用编译参数`moduleResolution`,指定使用哪一种方法。
251243

252-
没有指定定位方法时,就看原始脚本采用什么模块格式。如果模块格式是 CommonJS即编译时指定`--module commonjs`,那么模块定位采用 Node 方法,否则采用 Classic 方法(模块格式为 es2015、 esnext、amd, system, umd 等等)。
244+
没有指定定位方法时,就看原始脚本采用什么模块格式。如果模块格式是 CommonJS即编译时指定`--module commonjs`,那么模块定位采用 Node 方法,否则采用 Classic 方法(模块格式为 es2015、 esnext、amd, system, umd 等等)。
253245

254246
### 相对模块,非相对模块
255247

@@ -288,15 +280,15 @@ Node 方法就是模拟 Node.js 的模块加载方法。
288280
2. 当前目录的子目录`node_modules`,是否存在文件`package.json`,该文件的`types`字段是否指定了入口文件,如果是的就加载该文件。
289281
3. 当前目录的子目录`node_modules`里面,是否包含子目录`@types`,在该目录中查找文件`b.d.ts`
290282
4. 当前目录的子目录`node_modules`里面,是否包含子目录`b`,在该目录中查找`index.ts``index.tsx``index.d.ts`
291-
5. 进入上一层目录,重复上面4步,直到找到为止。
283+
5. 进入上一层目录,重复上面4步,直到找到为止。
292284

293285
### 路径映射
294286

295-
TypeScript 允许开发者在`tsconfig.json`文件里面,手动指定模块的路径
287+
TypeScript 允许开发者在`tsconfig.json`文件里面,手动指定脚本模块的路径
296288

297-
(1)baseUrl
289+
(1)baseUrl
298290

299-
`baseUrl`字段可以手动指定模块的基准目录
291+
`baseUrl`字段可以手动指定脚本模块的基准目录
300292

301293
```typescript
302294
{
@@ -310,7 +302,7 @@ TypeScript 允许开发者在`tsconfig.json`文件里面,手动指定模块的
310302

311303
(2)paths
312304

313-
`paths`字段指定非相对模块与实际脚本的映射
305+
`paths`字段指定非相对路径的模块与实际脚本的映射
314306

315307
```typescript
316308
{
@@ -373,3 +365,4 @@ $ tsc app.ts moduleA.ts --noResolve
373365
## 参考链接
374366

375367
- [tsconfig 之 importsNotUsedAsValues 属性](https://blog.51cto.com/u_13028258/5754309)
368+

‎docs/namespace.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ let t = new Shapes.Triangle();
164164

165165
// 写法二
166166
import * as shapes from "./shapes";
167-
let t = new shapes.Shapes.Triangle(); // shapes.Shapes?
167+
let t = new shapes.Shapes.Triangle();
168168
```
169169

170170
不过,更好的方法还是建议使用模块,采用模块的输出和输入。
@@ -300,3 +300,4 @@ namespace E {
300300
```
301301

302302
上面示例中,同名 Enum 与命名空间有同名成员,结果报错。
303+

0 commit comments

Comments
 (0)