Skip to content

Commit ac7c94e

Browse files
committed
init translation of module.d.ts
1 parent 8353b02 commit ac7c94e

File tree

1 file changed

+65
-73
lines changed

1 file changed

+65
-73
lines changed

docs/documentation/zh/declaration-files/templates/module.d.ts.md

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ layout: docs
44
permalink: /zh/docs/handbook/declaration-files/templates/module-d-ts.html
55
---
66

7-
## Comparing JavaScript to an example DTS
7+
## JavaScript 与示例 DTS 进行比较
88

9-
## Common CommonJS Patterns
9+
## 常见 CommonJS 模式
1010

11-
A module using CommonJS patterns uses `module.exports` to describe the exported values. For example, here is a module which exports a function and a numerical constant:
11+
使用 CommonJS 模式的模块使用 `module.exports` 来描述导出的值。例如,这里是导出函数和数值常量的模块示例:
1212

1313
```js
1414
const maxInterval = 12;
@@ -23,46 +23,45 @@ module.exports = {
2323
};
2424
```
2525

26-
This can be described by the following `.d.ts`:
26+
这可以通过以下 `.d.ts` 来描述:
2727

2828
```ts
2929
export function getArrayLength(arr: any[]): number;
3030
export const maxInterval: 12;
3131
```
3232

33-
The TypeScript playground can show you the `.d.ts` equivalent for JavaScript code. You can [try it yourself here](/play?useJavaScript=true#code/GYVwdgxgLglg9mABAcwKZQIICcsEMCeAMqmMlABYAUuOAlIgN6IBQiiW6IWSNWAdABsSZcswC+zCAgDOURAFtcADwAq5GKUQBeRAEYATM2by4AExBC+qJQAc4WKNO2NWKdNjxFhFADSvFquqk4sxAA).
33+
TypeScript 演练场可以展示 JavaScript 代码对应的 `.d.ts`。你可以[在这里自行尝试](/play?useJavaScript=true#code/GYVwdgxgLglg9mABAcwKZQIICcsEMCeAMqmMlABYAUuOAlIgN6IBQiiW6IWSNWAdABsSZcswC+zCAgDOURAFtcADwAq5GKUQBeRAEYATM2by4AExBC+qJQAc4WKNO2NWKdNjxFhFADSvFquqk4sxAA)
3434

35-
The `.d.ts` syntax intentionally looks like [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) syntax.
36-
ES Modules was ratified by TC39 in 2015 as part of ES2015 (ES6), while it has been available via transpilers for a long time, however if you have a JavaScript codebase using ES Modules:
35+
`.d.ts` 语法有意地看起来像 [ES 模块](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)语法。ES 模块在 2015 年由 TC39 正式通过,作为 ES2015(ES6)的一部分,尽管它已经通过转译器长时间可用,但如果你有一个使用 ES 模块的 JavaScript 代码库:
3736

3837
```js
3938
export function getArrayLength(arr) {
4039
return arr.length;
4140
}
4241
```
4342

44-
This would have the following `.d.ts` equivalent:
43+
这将具有以下 `.d.ts` 等效内容:
4544

4645
```ts
4746
export function getArrayLength(arr: any[]): number;
4847
```
4948

50-
### Default Exports
49+
### 默认导出
5150

52-
In CommonJS you can export any value as the default export, for example here is a regular expression module:
51+
CommonJS 中,你可以将任何值作为默认导出,例如下面是一个正则表达式模块:
5352

5453
```js
5554
module.exports = /hello( world)?/;
5655
```
5756

58-
Which can be described by the following .d.ts:
57+
可以通过以下 `.d.ts` 来描述:
5958

6059
```ts
6160
declare const helloWorld: RegExp;
6261
export default helloWorld;
6362
```
6463

65-
Or a number:
64+
或者一个数字:
6665

6766
```js
6867
module.exports = 3.142;
@@ -73,8 +72,7 @@ declare const pi: number;
7372
export default pi;
7473
```
7574

76-
One style of exporting in CommonJS is to export a function.
77-
Because a function is also an object, then extra fields can be added and are included in the export.
75+
CommonJS 中一种导出的方式是导出一个函数。因为函数也是一个对象,所以额外的字段可以被添加并包含在导出中。
7876

7977
```js
8078
function getArrayLength(arr) {
@@ -85,16 +83,14 @@ getArrayLength.maxInterval = 12;
8583
module.exports = getArrayLength;
8684
```
8785

88-
Which can be described with:
86+
可以这样描述:
8987

9088
```ts
9189
export default function getArrayLength(arr: any[]): number;
9290
export const maxInterval: 12;
9391
```
9492

95-
Note that using `export default` in your .d.ts files requires [`esModuleInterop: true`](/tsconfig#esModuleInterop) to work.
96-
If you can't have `esModuleInterop: true` in your project, such as when you're submitting a PR to Definitely Typed, you'll have to use the `export=` syntax instead. This older syntax is harder to use but works everywhere.
97-
Here's how the above example would have to be written using `export=`:
93+
请注意,在你的 .d.ts 文件中使用 `export default` 需要设置 [`esModuleInterop: true`](/zh/tsconfig#esModuleInterop) 才能正常工作。如果你的项目无法使用 `esModuleInterop: true`,比如当你向 Definitely Typed 提交 PR 时,你就不得不使用 `export=` 语法。这种较老的语法使用起来较困难,但在任何地方都可以正常工作。以下是如何使用 `export=` 来编写上述示例:
9894

9995
```ts
10096
declare function getArrayLength(arr: any[]): number;
@@ -105,11 +101,11 @@ declare namespace getArrayLength {
105101
export = getArrayLength;
106102
```
107103

108-
See [Module: Functions](/zh/docs/handbook/declaration-files/templates/module-function-d-ts.html) for details of how that works, and the [Modules reference](/zh/docs/handbook/modules.html) page.
104+
请查看[模块:函数](/zh/docs/handbook/declaration-files/templates/module-function-d-ts.html)以了解其工作原理的详细信息,以及[模块参考](/zh/docs/handbook/modules.html)页面。
109105

110-
## Handling Many Consuming Import
106+
## 处理多种导入方式
111107

112-
There are many ways to import a module in modern consuming code:
108+
在现代的消费者代码中,有许多导入模块的方式:
113109

114110
```ts
115111
const fastify = require("fastify");
@@ -121,8 +117,7 @@ import fastify from "fastify";
121117
import fastify, { FastifyInstance } from "fastify";
122118
```
123119

124-
Covering all of these cases requires the JavaScript code to actually support all of these patterns.
125-
To support many of these patterns, a CommonJS module would need to look something like:
120+
要涵盖所有这些情况,需要 JavaScript 代码实际上支持所有这些模式。为了支持其中多种模式,一个 CommonJS 模块需要类似以下形式:
126121

127122
```js
128123
class FastifyInstance {}
@@ -133,17 +128,17 @@ function fastify() {
133128

134129
fastify.FastifyInstance = FastifyInstance;
135130

136-
// Allows for { fastify }
131+
// 允许 { fastify }
137132
fastify.fastify = fastify;
138-
// Allows for strict ES Module support
133+
// 允许严格的 ES 模块支持
139134
fastify.default = fastify;
140-
// Sets the default export
135+
// 设置默认导出
141136
module.exports = fastify;
142137
```
143138

144-
## Types in Modules
139+
## 模块中的类型
145140

146-
You may want to provide a type for JavaScript code which does not exist
141+
你可能希望为 JavaScript 代码提供一个不存在的类型:
147142

148143
```js
149144
function getArrayMetadata(arr) {
@@ -158,7 +153,7 @@ module.exports = {
158153
};
159154
```
160155

161-
This can be described with:
156+
这可以用以下方式描述:
162157

163158
```ts
164159
export type ArrayMetadata = {
@@ -168,7 +163,7 @@ export type ArrayMetadata = {
168163
export function getArrayMetadata(arr: any[]): ArrayMetadata;
169164
```
170165

171-
This example is a good case for [using generics](/zh/docs/handbook/generics.html#generic-types) to provide richer type information:
166+
这个例子是一个很好的使用[泛型](/zh/docs/handbook/generics.html#generic-types)来提供更丰富类型信息的案例:
172167

173168
```ts
174169
export type ArrayMetadata<ArrType> = {
@@ -181,26 +176,24 @@ export function getArrayMetadata<ArrType>(
181176
): ArrayMetadata<ArrType>;
182177
```
183178

184-
Now the type of the array propagates into the `ArrayMetadata` type.
179+
现在数组的类型会传播到 `ArrayMetadata` 类型中。
185180

186-
The types which are exported can then be re-used by consumers of the modules using either `import` or `import type` in TypeScript code or [JSDoc imports](/zh/docs/handbook/jsdoc-supported-types.html#import-types).
181+
导出的类型可以被模块的消费者通过在 TypeScript 代码中使用 `import` `import type`,或者 [JSDoc 导入](/zh/docs/handbook/jsdoc-supported-types.html#import-types)来重复使用。
187182

188-
### Namespaces in Module Code
183+
### 模块代码中的命名空间
189184

190-
Trying to describe the runtime relationship of JavaScript code can be tricky.
191-
When the ES Module-like syntax doesn't provide enough tools to describe the exports then you can use `namespaces`.
185+
描述 JavaScript 代码的运行时关系可能有些棘手。当类似 ES 模块的语法无法提供足够的工具来描述导出时,你可以使用 `命名空间`
192186

193-
For example, you may have complex enough types to describe that you choose to namespace them inside your `.d.ts`:
187+
例如,你可能有足够复杂的类型需要描述,选择将它们放你的 `.d.ts` 文件的命名空间中:
194188

195189
```ts
196-
// This represents the JavaScript class which would be available at runtime
190+
// 这代表在运行时可用的 JavaScript
197191
export class API {
198192
constructor(baseURL: string);
199193
getInfo(opts: API.InfoRequest): API.InfoResponse;
200194
}
201195

202-
// This namespace is merged with the API class and allows for consumers, and this file
203-
// to have types which are nested away in their own sections.
196+
// 这个命名空间与 API 类合并,允许消费者和这个文件拥有被嵌套在自己部分中的类型。
204197
declare namespace API {
205198
export interface InfoRequest {
206199
id: string;
@@ -213,58 +206,57 @@ declare namespace API {
213206
}
214207
```
215208

216-
To understand how namespaces work in `.d.ts` files read the [`.d.ts` deep dive](/zh/docs/handbook/declaration-files/deep-dive.html).
209+
要了解命名空间在 `.d.ts` 文件中的工作原理,请阅读 [`.d.ts` 深入研究](/zh/docs/handbook/declaration-files/deep-dive.html)
217210

218-
### Optional Global Usage
211+
### 可选全局使用
219212

220-
You can use `export as namespace` to declare that your module will be available in the global scope in UMD contexts:
213+
你可以使用 `export as namespace` 来声明你的模块将在 UMD 上下文中以全局范围可用:
221214

222215
```ts
223216
export as namespace moduleName;
224217
```
225218

226-
## Reference Example
219+
## 参考示例
227220

228-
To give you an idea of how all these pieces can come together, here is a reference `.d.ts` to start with when making a new module
221+
为了让你了解所有这些部分如何结合在一起,这里是一个参考的 `.d.ts`,可以在创建新模块时使用。
229222

230223
```ts
231-
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
232-
// Project: [~THE PROJECT NAME~]
233-
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
234-
235-
/*~ This is the module template file. You should rename it to index.d.ts
236-
*~ and place it in a folder with the same name as the module.
237-
*~ For example, if you were writing a file for "super-greeter", this
238-
*~ file should be 'super-greeter/index.d.ts'
224+
// 类型定义 for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
225+
// 项目: [~THE PROJECT NAME~]
226+
// 定义者: [~YOUR NAME~] <[~A URL FOR YOU~]>
227+
228+
/*~ 这是模块模板文件。你应该将其重命名为 index.d.ts
229+
*~ 并将其放在与模块同名的文件夹中。
230+
*~ 例如,如果你正在为 "super-greeter" 编写文件,那么
231+
*~ 文件应该是 'super-greeter/index.d.ts'
239232
*/
240233

241-
/*~ If this module is a UMD module that exposes a global variable 'myLib' when
242-
*~ loaded outside a module loader environment, declare that global here.
243-
*~ Otherwise, delete this declaration.
234+
/*~ 如果此模块是一个 UMD 模块,在加载到模块加载器环境之外时会暴露一个全局变量 'myLib',请在这里声明全局变量。
235+
*~ 否则,请删除此声明。
244236
*/
245237
export as namespace myLib;
246238

247-
/*~ If this module exports functions, declare them like so.
239+
/*~ 如果此模块导出函数,请这样声明。
248240
*/
249241
export function myFunction(a: string): string;
250242
export function myOtherFunction(a: number): number;
251243

252-
/*~ You can declare types that are available via importing the module */
244+
/*~ 你可以声明通过导入模块可用的类型 */
253245
export interface SomeType {
254246
name: string;
255247
length: number;
256248
extras?: string[];
257249
}
258250

259-
/*~ You can declare properties of the module using const, let, or var */
251+
/*~ 你可以使用 constlet var 声明模块的属性 */
260252
export const myField: number;
261253
```
262254

263-
### Library file layout
255+
### 库文件布局
264256

265-
The layout of your declaration files should mirror the layout of the library.
257+
你的声明文件的布局应该与库的布局相对应。
266258

267-
A library can consist of multiple modules, such as
259+
一个库可以由多个模块组成,比如
268260

269261
```
270262
myLib
@@ -275,7 +267,7 @@ myLib
275267
+---- baz.js
276268
```
277269

278-
These could be imported as
270+
这些可以被导入为
279271

280272
```js
281273
var a = require("myLib");
@@ -284,7 +276,7 @@ var c = require("myLib/bar");
284276
var d = require("myLib/bar/baz");
285277
```
286278

287-
Your declaration files should thus be
279+
因此,你的声明文件应该是
288280

289281
```
290282
@types/myLib
@@ -295,17 +287,17 @@ Your declaration files should thus be
295287
+---- baz.d.ts
296288
```
297289

298-
### Testing your types
290+
### 测试你的类型
299291

300-
If you are planning on submitting these changes to DefinitelyTyped for everyone to also use, then we recommend you:
292+
如果你计划将这些更改提交给 DefinitelyTyped,以供其他人使用,那么我们建议你:
301293

302-
> 1. Create a new folder in `node_modules/@types/[libname]`
303-
> 2. Create an `index.d.ts` in that folder, and copy the example in
304-
> 3. See where your usage of the module breaks, and start to fill out the index.d.ts
305-
> 4. When you're happy, clone [DefinitelyTyped/DefinitelyTyped](https://github.com/DefinitelyTyped) and follow the instructions in the README.
294+
> 1. `node_modules/@types/[libname]` 中创建一个新文件夹
295+
> 2. 在该文件夹中创建一个 `index.d.ts`,并将示例复制进去
296+
> 3. 查看你对模块的使用出现问题的地方,并开始填写 index.d.ts
297+
> 4. 当你满意时,克隆 [DefinitelyTyped/DefinitelyTyped](https://github.com/DefinitelyTyped) 并按照 README 中的说明操作。
306298
307-
Otherwise
299+
否则
308300

309-
> 1. Create a new file in the root of your source tree: `[libname].d.ts`
310-
> 2. Add `declare module "[libname]" { }`
311-
> 3. Add the template inside the braces of the declare module, and see where your usage breaks
301+
> 1. 在你源代码树的根目录中创建一个新文件:`[libname].d.ts`
302+
> 2. 添加 `declare module "[libname]" { }`
303+
> 3. declare module 的大括号内添加模板,并查看你的使用出现问题的地方

0 commit comments

Comments
 (0)