@@ -4,64 +4,62 @@ layout: docs
4
4
permalink : /zh/docs/handbook/declaration-files/templates/module-plugin-d-ts.html
5
5
---
6
6
7
- For example, when you want to work with JavaScript code which extends another library.
7
+ 例如,当你想要使用扩展另一个库的 JavaScript 代码时。
8
8
9
9
``` ts
10
10
import { greeter } from " super-greeter" ;
11
11
12
- // Normal Greeter API
12
+ // 正常的问候 API
13
13
greeter (2 );
14
14
greeter (" Hello world" );
15
15
16
- // Now we extend the object with a new function at runtime
16
+ // 现在我们在运行时用一个新函数扩展对象
17
17
import " hyper-super-greeter" ;
18
18
greeter .hyperGreet ();
19
19
```
20
20
21
- The definition for " super-greeter":
21
+ 对于“ super-greeter”的定义:
22
22
23
23
``` ts
24
- /* ~ This example shows how to have multiple overloads for your function */
24
+ /* ~ 此示例展示了如何让函数有多个重载 */
25
25
export interface GreeterFunction {
26
26
(name : string ): void
27
27
(time : number ): void
28
28
}
29
29
30
- /* ~ This example shows how to export a function specified by an interface */
30
+ /* ~ 此示例展示了如何导出由接口指定的函数 */
31
31
export const greeter: GreeterFunction ;
32
32
```
33
33
34
- We can extend the existing module like the following:
34
+ 我们可以像下面这样扩展现有模块:
35
35
36
36
``` ts
37
- // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
38
- // Project : [~THE PROJECT NAME~]
39
- // Definitions by : [~YOUR NAME~] <[~A URL FOR YOU~]>
37
+ // 类型定义为 [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
38
+ // 项目 : [~THE PROJECT NAME~]
39
+ // 定义者 : [~YOUR NAME~] <[~A URL FOR YOU~]>
40
40
41
- /* ~ This is the module plugin template file. You should rename it to index.d.ts
42
- *~ and place it in a folder with the same name as the module.
43
- *~ For example, if you were writing a file for "super-greeter", this
44
- *~ file should be 'super-greeter/index.d.ts'
41
+ /* ~ 这是模块插件模板文件。你应该将其重命名为 index.d.ts
42
+ *~ 并将其放在与模块同名的文件夹中。
43
+ *~ 例如,如果你为 "super-greeter" 编写一个文件,此
44
+ *~ 文件应该是 'super-greeter/index.d.ts'
45
45
*/
46
46
47
- /* ~ On this line, import the module which this module adds to */
47
+ /* ~ 在此行上,导入此模块添加的模块 */
48
48
import { greeter } from " super-greeter" ;
49
49
50
- /* ~ Here, declare the same module as the one you imported above
51
- *~ then we expand the existing declaration of the greeter function
50
+ /* ~ 在此处,声明与上面导入的模块相同的模块
51
+ *~ 然后我们扩展 greeter 函数的现有声明
52
52
*/
53
53
export module " super-greeter" {
54
54
export interface GreeterFunction {
55
- /** Greets even better! */
55
+ /** 更好的问候! */
56
56
hyperGreet(): void ;
57
57
}
58
58
}
59
59
```
60
60
61
- This uses [ declaration merging ] ( /zh/docs/handbook/declaration-merging.html )
61
+ 这使用了 [ 声明合并 ] ( /zh/docs/handbook/declaration-merging.html )
62
62
63
- ## The Impact of ES6 on Module Plugins
63
+ ## ES6 对模块插件的影响
64
64
65
- Some plugins add or modify top-level exports on existing modules.
66
- While this is legal in CommonJS and other loaders, ES6 modules are considered immutable and this pattern will not be possible.
67
- Because TypeScript is loader-agnostic, there is no compile-time enforcement of this policy, but developers intending to transition to an ES6 module loader should be aware of this.
65
+ 有些插件在现有模块上添加或修改顶级导出。虽然这在 CommonJS 和其他加载器中是合法的,但 ES6 模块被认为是不可变的,这种模式将不再可能。由于 TypeScript 是与加载器无关的,因此没有编译时强制执行此策略,但打算迁移到 ES6 模块加载器的开发人员应该注意这一点。
0 commit comments