1
1
---
2
- title : " Module: Function "
2
+ title : " 模块: 函数 "
3
3
layout : docs
4
4
permalink : /zh/docs/handbook/declaration-files/templates/module-function-d-ts.html
5
5
---
6
6
7
- For example, when you want to work with JavaScript code which looks like:
7
+ # 模块: 函数
8
+
9
+ 当你想处理如下 JavaScript 代码时:
8
10
9
11
``` ts
10
12
import greeter from " super-greeter" ;
@@ -13,56 +15,53 @@ greeter(2);
13
15
greeter (" Hello world" );
14
16
```
15
17
16
- To handle both importing via UMD and modules:
18
+ 为了同时支持 UMD 和模块导入,你可以使用以下模板:
17
19
18
20
``` ts
19
- // Type definitions for [~THE LIBRARY NAME ~] [~OPTIONAL VERSION NUMBER~]
20
- // Project : [~THE PROJECT NAME ~]
21
- // Definitions by : [~YOUR NAME ~] <[~A URL FOR YOU ~]>
21
+ // [~库名称 ~] [~可选版本号~] 的类型定义
22
+ // 项目 : [~项目名称 ~]
23
+ // 定义者 : [~你的姓名 ~] <[~你的网址 ~]>
22
24
23
- /* ~ This is the module template file for function modules.
24
- *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
25
- *~ For example, if you were writing a file for "super-greeter", this
26
- *~ file should be 'super-greeter/index.d.ts'
25
+ /* ~ 这是函数模块的模块模板文件。
26
+ *~ 你应该将其重命名为 index.d.ts 并将其放在与模块同名的文件夹中。
27
+ *~ 例如,如果你为 "super-greeter" 编写文件,则
28
+ *~ 此文件应为 'super-greeter/index.d.ts'
27
29
*/
28
30
29
- // Note that ES6 modules cannot directly export class objects.
30
- // This file should be imported using the CommonJS-style:
31
- // import x = require('[~THE MODULE ~]');
31
+ // 注意, ES6 模块无法直接导出类对象。
32
+ // 此文件应使用 CommonJS 风格导入:
33
+ // import x = require('[~模块 ~]');
32
34
//
33
- // Alternatively, if --allowSyntheticDefaultImports or
34
- // --esModuleInterop is turned on, this file can also be
35
- // imported as a default import:
36
- // import x from '[~THE MODULE~]';
35
+ // 或者,如果启用了 --allowSyntheticDefaultImports 或
36
+ // --esModuleInterop,可以将此文件作为默认导入:
37
+ // import x from '[~模块~]';
37
38
//
38
- // Refer to the TypeScript documentation at
39
+ // 请参阅 TypeScript 文档了解
39
40
// https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
40
- // to understand common workarounds for this limitation of ES6 modules.
41
+ // 以了解此 ES6 模块限制的常见解决方法。
41
42
42
- /* ~ If this module is a UMD module that exposes a global variable 'myFuncLib' when
43
- *~ loaded outside a module loader environment, declare that global here.
44
- *~ Otherwise, delete this declaration.
43
+ /* ~ 如果该模块是一个 UMD 模块,当在模块加载器环境之外加载时
44
+ *~ 会暴露一个全局变量 'myFuncLib',在此处声明该全局变量。
45
+ *~ 否则,请删除此声明。
45
46
*/
46
47
export as namespace myFuncLib ;
47
48
48
- /* ~ This declaration specifies that the function
49
- *~ is the exported object from the file
50
- */
49
+ /* ~ 此声明指定函数是从文件中导出的对象 */
51
50
export = Greeter ;
52
51
53
- /* ~ This example shows how to have multiple overloads for your function */
52
+ /* ~ 该示例显示如何为函数定义多个重载 */
54
53
declare function Greeter(name : string ): Greeter .NamedReturnType ;
55
54
declare function Greeter(length : number ): Greeter .LengthReturnType ;
56
55
57
- /* ~ If you want to expose types from your module as well, you can
58
- *~ place them in this block. Often you will want to describe the
59
- *~ shape of the return type of the function; that type should
60
- *~ be declared in here, as this example shows.
56
+ /* ~ 如果你还想暴露模块中的类型,可以
57
+ *~ 将它们放在此块中。通常你会想描述
58
+ *~ 函数的返回类型的形状;该类型应
59
+ *~ 在这里声明,如此示例所示。
61
60
*~
62
- *~ Note that if you decide to include this namespace, the module can be
63
- *~ incorrectly imported as a namespace object, unless
64
- *~ --esModuleInterop is turned on:
65
- *~ import * as x from '[~THE MODULE ~]'; // WRONG! DO NOT DO THIS!
61
+ *~ 注意,如果你决定包含此命名空间,模块可能会
62
+ *~ 被错误地作为命名空间对象导入,除非
63
+ *~ 启用了 --esModuleInterop:
64
+ *~ import * as x from '[~模块 ~]'; // 错误!不要这样做!
66
65
*/
67
66
declare namespace Greeter {
68
67
export interface LengthReturnType {
@@ -74,8 +73,8 @@ declare namespace Greeter {
74
73
lastName: string ;
75
74
}
76
75
77
- /* ~ If the module also has properties, declare them here. For example,
78
- *~ this declaration says that this code is legal:
76
+ /* ~ 如果模块还有属性,请在此声明。例如,
77
+ *~ 此声明表示以下代码是合法的:
79
78
*~ import f = require('super-greeter');
80
79
*~ console.log(f.defaultName);
81
80
*/
0 commit comments