Skip to content

Commit 2b4d8a4

Browse files
authored
Merge branch 'main' into deep-dive
2 parents b73275d + 539346d commit 2b4d8a4

File tree

2 files changed

+44
-65
lines changed

2 files changed

+44
-65
lines changed

docs/documentation/zh/javascript/Intro to JS with TS.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ translatable: true
99
在一般的代码中,TypeScript 中的类型系统的严格程度是不一样的:
1010

1111
- 只基于 JavaScript 代码推断
12-
- 增强的 JavaScript 类型推断([通过 JSDoc](/zh/docs/handbook/jsdoc-supported-types.html)
12+
- 增强的 JavaScript 类型推断([通过 JSDoc](/zh/docs/handbook/jsdoc-supported-types.html)
1313
- 在 JavaScript 文件里面使用 `// @ts-check`
1414
- TypeScript 代码
15-
- TypeScript 的 [`strict`](/tsconfig#strict) 模式
15+
- TypeScript 的 [`strict`](/zh/tsconfig#strict) 模式
1616

1717
每一步都代表着向更安全的类型系统迈进,但并不是每个项目都需要级别最高的验证。
1818

1919
## TypeScript 与 JavaScript
2020

21-
在使用某些编辑器时,它可以为使用 TypeScript 提供工具,如自动补全,跳转到特定位置。还有一些重构工具,如重命名。
22-
[主页](/) 有一个包含 TypeScript 插件的编辑器列表。
21+
某些编辑器会使用 TypeScript 来提供帮助,如自动补全,跳转到特定位置。此外还可以帮助重构,如重命名。[主页](/zh)有一个编辑器列表,该列表中的编辑器包含 TypeScript 插件。
2322

2423
## 通过 JSDoc 在 JS 中提供类型提示
2524

2625
`.js` 文件里,类型通常可以被推断出来。当无法推断类型时,可以使用 JSDoc 语法来指定。
2726

28-
声明之前的 JSDoc 注释,将用于设置该声明的类型。例如:
27+
位于声明语句之前的 JSDoc 注释,将用于设置该声明的类型。例如:
2928

3029
```js twoslash
3130
/** @type {number} */
@@ -35,12 +34,11 @@ x = 0; // OK
3534
x = false; // OK?!
3635
```
3736

38-
你可以[JSDoc 支持的类型列表](/zh/docs/handbook/jsdoc-supported-types.html)找到支持的 JSDoc 模式的完整说明
37+
你可以在 [JSDoc 支持的类型列表](/zh/docs/handbook/jsdoc-supported-types.html)找到受支持的 JSDoc 语法的完整说明
3938

4039
## `@ts-check`
4140

42-
上述代码示例的最后一行会在 TypeScript 中引发错误,但在 JS 项目中,默认情况下不会引发错误。
43-
要在 JavaScript 文件显示错误,请在 `.js` 文件的第一行中添加: `// @ts-check`
41+
上述代码示例的最后一行在 TypeScript 中会引发错误,但在 JS 项目中,默认情况下不会引发错误。要使 JavaScript 文件显示错误,请在 `.js` 文件的第一行中添加: `// @ts-check`
4442

4543
```js twoslash
4644
// @ts-check
@@ -52,10 +50,9 @@ x = 0; // OK
5250
x = false; // Not OK
5351
```
5452

55-
如果你有许多 JavaScript 文件要显示错误,则可以使用 [`jsconfig.json`](/zh/docs/handbook/tsconfig-json.html) 来代替注释。
56-
你可以通过在文件中添加`// @ts-nocheck` 注释来跳过检查某些文件。
53+
如果你有许多 JavaScript 文件要显示错误,则可以使用 [`jsconfig.json`](/zh/docs/handbook/tsconfig-json.html) 来代替注释。你可以通过在文件中添加 `// @ts-nocheck` 注释来跳过检查某些文件。
5754

58-
TypeScript 可能会提供你不想看到的错误,在这种情况下,你可以通过在前一行添加 `// @ts-ignore``// @ts-expect-error` 来忽略某一行上的错误
55+
TypeScript 可能会提供一些你不想看到的错误,在这种情况下,你可以通过在某一行的前一行添加 `// @ts-ignore``// @ts-expect-error` 来忽略该行上的错误
5956

6057
```js twoslash
6158
// @ts-check
@@ -67,4 +64,4 @@ x = 0; // OK
6764
x = false; // Not OK
6865
```
6966

70-
要了解有关 TypeScript 如何解释 JavaScript 的更多信息,请阅读 [TS 是如何解释 JS 的](/zh/docs/handbook/type-checking-javascript-files.html)
67+
要详细了解 TypeScript 如何解释 JavaScript,请阅读 [TS 是如何检查 JS 的](/zh/docs/handbook/type-checking-javascript-files.html)

docs/documentation/zh/reference/Triple-Slash Directives.md

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,62 @@
11
---
2-
title: Triple-Slash Directives
2+
title: 三斜线指令
33
layout: docs
44
permalink: /zh/docs/handbook/triple-slash-directives.html
5-
oneline: How to use triple slash directives in TypeScript
5+
oneline: 如何在 TypeScript 中使用三斜线指令
66
translatable: true
77
---
88

9-
Triple-slash directives are single-line comments containing a single XML tag.
10-
The contents of the comment are used as compiler directives.
9+
三斜线指令是包含单个 XML 标记的单行注释,其内容用于提供编译器指令。
1110

12-
Triple-slash directives are **only** valid at the top of their containing file.
13-
A triple-slash directive can only be preceded by single or multi-line comments, including other triple-slash directives.
14-
If they are encountered following a statement or a declaration they are treated as regular single-line comments, and hold no special meaning.
11+
请注意,三斜线指令****在所在文件的顶部有效。这些指令只能位于单行或多行注释之前,包括其他三斜线指令。如果它们出现在语句或声明之后,则会被视为常规单行注释,不具有特殊含义。
1512

1613
## `/// <reference path="..." />`
1714

18-
The `/// <reference path="..." />` directive is the most common of this group.
19-
It serves as a declaration of _dependency_ between files.
15+
`/// <reference path="..." />` 指令是其中最常见的。它用作文件之间的*依赖*声明。
2016

21-
Triple-slash references instruct the compiler to include additional files in the compilation process.
17+
三斜线引用指示编译器在编译过程中包含其他文件。
2218

23-
They also serve as a method to order the output when using [`out`](/tsconfig#out) or [`outFile`](/tsconfig#outFile).
24-
Files are emitted to the output file location in the same order as the input after preprocessing pass.
19+
此外,当使用 [`out`](/zh/tsconfig#out)[`outFile`](/zh/tsconfig#outFile) 时,它们还可以用来对输出进行排序。预处理完成后,文件会按照输入的顺序写入到输出文件位置。
2520

26-
### Preprocessing input files
21+
### 预处理输入文件
2722

28-
The compiler performs a preprocessing pass on input files to resolve all triple-slash reference directives.
29-
During this process, additional files are added to the compilation.
23+
在编译过程中,编译器会对输入文件执行预处理,以解析所有三斜线引用指令,并将其他文件添加到编译中。
3024

31-
The process starts with a set of _root files_;
32-
these are the file names specified on the command-line or in the [`files`](/tsconfig#files) list in the `tsconfig.json` file.
33-
These root files are preprocessed in the same order they are specified.
34-
Before a file is added to the list, all triple-slash references in it are processed, and their targets included.
35-
Triple-slash references are resolved in a depth-first manner, in the order they have been seen in the file.
25+
该过程从一组*根文件*开始,这些根文件是在命令行或 `tsconfig.json` 文件的 [`files`](/zh/tsconfig#files) 列表中指定的文件名。根文件按照它们被指定的顺序进行预处理。在将文件添加到列表之前,会解析其中的所有三斜线引用指令,并包含它们的目标文件。三斜线引用指令按照深度优先的顺序解析,依据它们在文件中的出现顺序进行处理。
3626

37-
A triple-slash reference path is resolved relative to the containing file, if a relative path is used.
27+
当使用相对路径时,三斜线引用路径将相对于包含该引用的文件进行解析。
3828

39-
### Errors
29+
### 错误
4030

41-
It is an error to reference a file that does not exist.
42-
It is an error for a file to have a triple-slash reference to itself.
31+
引用不存在的文件将导致错误。文件包含对自身的三斜线引用也会导致错误。
4332

44-
### Using `--noResolve`
33+
### 使用 `--noResolve`
4534

46-
If the compiler flag [`noResolve`](/tsconfig#noResolve) is specified, triple-slash references are ignored; they neither result in adding new files, nor change the order of the files provided.
35+
如果指定了编译器标志 [`noResolve`](/zh/tsconfig#noResolve),则三斜线引用会被忽略,不会添加新文件,也不会改变提供文件的顺序。
4736

4837
## `/// <reference types="..." />`
4938

50-
Similar to a `/// <reference path="..." />` directive, which serves as a declaration of _dependency_, a `/// <reference types="..." />` directive declares a dependency on a package.
39+
`/// <reference path="..." />` 指令用作*依赖*声明类似,`/// <reference types="..." />` 指令声明对包的依赖。
5140

52-
The process of resolving these package names is similar to the process of resolving module names in an `import` statement.
53-
An easy way to think of triple-slash-reference-types directives are as an `import` for declaration packages.
41+
解析这些包名称的过程类似于解析 `import` 语句中的模块名称的过程。可以将三斜线引用类型指令视为声明包的简单 `import` 方式。
5442

55-
For example, including `/// <reference types="node" />` in a declaration file declares that this file uses names declared in `@types/node/index.d.ts`;
56-
and thus, this package needs to be included in the compilation along with the declaration file.
43+
例如,声明文件中包含 `/// <reference types="node" />` 表示该文件使用在 `@types/node/index.d.ts` 中声明的名称;因此,需要将此包与声明文件一起包含在编译中。
5744

58-
Use these directives only when you're authoring a `d.ts` file by hand.
45+
这些指令仅在手动编写 `d.ts` 文件时使用。
5946

60-
For declaration files generated during compilation, the compiler will automatically add `/// <reference types="..." />` for you;
61-
A `/// <reference types="..." />` in a generated declaration file is added _if and only if_ the resulting file uses any declarations from the referenced package.
47+
在编译过程中生成的声明文件中,编译器会自动为你添加 `/// <reference types="..." />`。在生成的声明文件中,仅当结果文件使用了来自所引用包的任何声明时,才会添加 `/// <reference types="..." />`
6248

63-
For declaring a dependency on an `@types` package in a `.ts` file, use [`types`](/tsconfig#types) on the command line or in your `tsconfig.json` instead.
64-
See [using `@types`, `typeRoots` and `types` in `tsconfig.json` files](/zh/docs/handbook/tsconfig-json.html#types-typeroots-and-types) for more details.
49+
要在 `.ts` 文件中声明对 `@types` 包的依赖,请在命令行或 `tsconfig.json` 文件中使用 [`types`](/tsconfig#types)。有关更多详细信息,请参阅[`tsconfig.json` 文件中使用 `@types``typeRoots``types`](/zh/docs/handbook/tsconfig-json.html#types-typeroots-and-types)
6550

6651
## `/// <reference lib="..." />`
6752

68-
This directive allows a file to explicitly include an existing built-in _lib_ file.
53+
该指令允许文件显式包含现有的内置 _lib_ 文件。
6954

70-
Built-in _lib_ files are referenced in the same fashion as the [`lib`](/tsconfig#lib) compiler option in _tsconfig.json_ (e.g. use `lib="es2015"` and not `lib="lib.es2015.d.ts"`, etc.).
55+
引用内置 _lib_ 文件的方式与 _tsconfig.json_ 中的 [`lib`](/zh/tsconfig#lib) 编译器选项相同(例如使用 `lib="es2015"` 而不是 `lib="lib.es2015.d.ts"`)。
7156

72-
For declaration file authors who rely on built-in types, e.g. DOM APIs or built-in JS run-time constructors like `Symbol` or `Iterable`, triple-slash-reference lib directives are recommended. Previously these .d.ts files had to add forward/duplicate declarations of such types.
57+
对于依赖内置类型的声明文件作者,例如 DOM API 或内置 JS 运行时构造函数(如 `Symbol` `Iterable`),建议使用三斜线引用 lib 指令。以前,这些 .d.ts 文件必须添加这些类型的前向/重复声明。
7358

74-
For example, adding `/// <reference lib="es2017.string" />` to one of the files in a compilation is equivalent to compiling with `--lib es2017.string`.
59+
例如,在编译中的文件中添加 `/// <reference lib="es2017.string" />` 相当于使用 `--lib es2017.string` 进行编译。
7560

7661
```ts
7762
/// <reference lib="es2017.string" />
@@ -81,20 +66,17 @@ For example, adding `/// <reference lib="es2017.string" />` to one of the files
8166

8267
## `/// <reference no-default-lib="true"/>`
8368

84-
This directive marks a file as a _default library_.
85-
You will see this comment at the top of `lib.d.ts` and its different variants.
69+
该指令将文件标记为*默认库*。你会在 `lib.d.ts` 及其变体的顶部看到此注释。
8670

87-
This directive instructs the compiler to _not_ include the default library (i.e. `lib.d.ts`) in the compilation.
88-
The impact here is similar to passing [`noLib`](/tsconfig#noLib) on the command line.
71+
此指令指示编译器在编译过程中*不要*包含默认库(即 `lib.d.ts`)。其效果类似于在命令行上传递 [`noLib`](/zh/tsconfig#noLib)
8972

90-
Also note that when passing [`skipDefaultLibCheck`](/tsconfig#skipDefaultLibCheck), the compiler will only skip checking files with `/// <reference no-default-lib="true"/>`.
73+
此外,请注意,当传递 [`skipDefaultLibCheck`](/zh/tsconfig#skipDefaultLibCheck) 时,编译器将仅跳过带有 `/// <reference no-default-lib="true"/>` 注释的文件的检查。
9174

9275
## `/// <amd-module />`
9376

94-
By default AMD modules are generated anonymous.
95-
This can lead to problems when other tools are used to process the resulting modules, such as bundlers (e.g. `r.js`).
77+
默认情况下,会生成匿名的 AMD 模块。当使用其他工具处理生成的模块时(例如打包工具 `r.js`),这可能会导致问题。
9678

97-
The `amd-module` directive allows passing an optional module name to the compiler:
79+
`amd-module` 指令允许向编译器传递一个可选的模块名称:
9880

9981
##### amdModule.ts
10082

@@ -103,7 +85,7 @@ The `amd-module` directive allows passing an optional module name to the compile
10385
export class C {}
10486
```
10587

106-
Will result in assigning the name `NamedModule` to the module as part of calling the AMD `define`:
88+
将导致在调用 AMD `define` 时将名称 `NamedModule` 分配给模块:
10789

10890
##### amdModule.js
10991

@@ -119,19 +101,19 @@ define("NamedModule", ["require", "exports"], function (require, exports) {
119101

120102
## `/// <amd-dependency />`
121103

122-
> **Note**: this directive has been deprecated. Use `import "moduleName";` statements instead.
104+
> **注意**: 此指令已被弃用。请改用 `import "moduleName";` 语句。
123105
124-
`/// <amd-dependency path="x" />` informs the compiler about a non-TS module dependency that needs to be injected in the resulting module's require call.
106+
`/// <amd-dependency path="x" />` 通知编译器有一个非 TypeScript 模块依赖项需要注入到生成模块的 require 调用中。
125107

126-
The `amd-dependency` directive can also have an optional `name` property; this allows passing an optional name for an amd-dependency:
108+
`amd-dependency` 指令也可以具有可选的 `name` 属性;这允许为 amd-dependency 传递一个可选名称:
127109

128110
```ts
129111
/// <amd-dependency path="legacy/moduleA" name="moduleA"/>
130112
declare var moduleA: MyType;
131113
moduleA.callStuff();
132114
```
133115

134-
Generated JS code:
116+
生成的 JS 代码:
135117

136118
```js
137119
define(["require", "exports", "legacy/moduleA"], function (

0 commit comments

Comments
 (0)