1
1
---
2
- title : " Module: Class "
3
- layout : docs
2
+ title : " 模块:类 "
3
+ layout : 文档
4
4
permalink : /zh/docs/handbook/declaration-files/templates/module-class-d-ts.html
5
5
---
6
6
7
7
<!--
8
8
TODO:
9
9
10
- 1. Not clear why UMD is thrown in here.
11
- 2. Give both commonjs and ES module examples.
10
+ 1. 不清楚为什么在这里加入 UMD。
11
+ 2. 给出 CommonJS 和 ES 模块的示例。
12
12
-->
13
13
14
- For example, when you want to work with JavaScript code which looks like:
14
+ 例如,当你希望使用类似以下 JavaScript 代码时:
15
15
16
16
``` ts
17
17
const Greeter = require (" super-greeter" );
@@ -20,44 +20,43 @@ const greeter = new Greeter();
20
20
greeter .greet ();
21
21
```
22
22
23
- To handle both importing via UMD and modules:
23
+ 处理通过 UMD 和模块导入的情况:
24
24
25
25
``` ts
26
- // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
27
- // Project : [~THE PROJECT NAME~]
28
- // Definitions by : [~YOUR NAME~] <[~A URL FOR YOU~]>
26
+ // 类型定义为 [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
27
+ // 项目 : [~THE PROJECT NAME~]
28
+ // 定义者 : [~YOUR NAME~] <[~A URL FOR YOU~]>
29
29
30
- /* ~ This is the module template file for class modules.
31
- *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
32
- *~ For example, if you were writing a file for "super-greeter", this
33
- *~ file should be 'super-greeter/index.d.ts'
30
+ /* ~ 这是用于类模块的模块模板文件。
31
+ *~ 你应该将其重命名为 index.d.ts,并将其放在与模块同名的文件夹中。
32
+ *~ 例如,如果你为 "super-greeter" 编写一个文件,此
33
+ *~ 文件应该是 'super-greeter/index.d.ts'
34
34
*/
35
35
36
- // Note that ES6 modules cannot directly export class objects.
37
- // This file should be imported using the CommonJS-style:
36
+ // 请注意, ES6 模块无法直接导出类对象。
37
+ // 该文件应该使用 CommonJS 风格导入:
38
38
// import x = require('[~THE MODULE~]');
39
39
//
40
- // Alternatively, if --allowSyntheticDefaultImports or
41
- // --esModuleInterop is turned on, this file can also be
42
- // imported as a default import:
40
+ // 或者,如果 --allowSyntheticDefaultImports 或
41
+ // --esModuleInterop 打开,该文件也可以作为默认导入导入:
43
42
// import x from '[~THE MODULE~]';
44
43
//
45
- // Refer to the TypeScript documentation at
44
+ // 请参阅 TypeScript 文档
46
45
// https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
47
- // to understand common workarounds for this limitation of ES6 modules.
46
+ // 了解 ES6 模块的此限制的常见解决方法。
48
47
49
- /* ~ If this module is a UMD module that exposes a global variable 'myClassLib' when
50
- *~ loaded outside a module loader environment, declare that global here.
51
- *~ Otherwise, delete this declaration.
48
+ /* ~ 如果此模块是一个 UMD 模块,在加载到模块加载器环境之外时公开全局变量 'myClassLib',
49
+ *~ 请在此处声明该全局变量。
50
+ *~ 否则,请删除此声明。
52
51
*/
53
52
export as namespace " super-greeter" ;
54
53
55
- /* ~ This declaration specifies that the class constructor function
56
- *~ is the exported object from the file
54
+ /* ~ 此声明指定类构造函数
55
+ *~ 是文件中导出的对象
57
56
*/
58
57
export = Greeter ;
59
58
60
- /* ~ Write your module's methods and properties in this class */
59
+ /* ~ 在此类中编写你模块的方法和属性 */
61
60
declare class Greeter {
62
61
constructor (customGreeting ? : string );
63
62
@@ -66,13 +65,13 @@ declare class Greeter {
66
65
myMethod(opts : MyClass .MyClassMethodOptions ): number ;
67
66
}
68
67
69
- /* ~ If you want to expose types from your module as well, you can
70
- *~ place them in this block.
68
+ /* ~ 如果你还想公开模块的类型,你可以
69
+ *~ 将它们放在此块中。
71
70
*~
72
- *~ Note that if you decide to include this namespace, the module can be
73
- *~ incorrectly imported as a namespace object, unless
74
- *~ --esModuleInterop is turned on:
75
- *~ import * as x from '[~THE MODULE~]'; // WRONG! DO NOT DO THIS!
71
+ *~ 请注意,如果决定包含此命名空间,则可能会
72
+ *~ 错误地将模块导入为命名空间对象,除非
73
+ *~ 打开了 --esModuleInterop:
74
+ *~ import * as x from '[~THE MODULE~]'; // 错误!不要这样做!
76
75
*/
77
76
declare namespace MyClass {
78
77
export interface MyClassMethodOptions {
0 commit comments