@@ -4,7 +4,7 @@ layout: docs
44permalink : /zh/docs/handbook/declaration-files/templates/global-d-ts.html
55---
66
7- ## Global Libraries
7+ ## 全局库
88
99<!--
1010TODO:
@@ -17,128 +17,53 @@ TODO:
1717
1818-->
1919
20- A _ global_ library is one that can be accessed from the global scope (i.e. without using any form of ` import ` ).
21- Many libraries simply expose one or more global variables for use.
22- For example, if you were using [ jQuery] ( https://jquery.com/ ) , the ` $ ` variable can be used by simply referring to it:
20+ 全局库是可以从全局范围访问的库(即不使用任何形式的 ` import ` )。许多库只是简单地公开一个或多个全局变量供使用。例如,如果你正在使用 [ jQuery] ( https://jquery.com/ ) ,则可以通过简单地引用 ` $ ` 变量来使用:
2321
2422``` ts
2523$ (() => {
26- console .log (" hello! " );
24+ console .log (" 你好! " );
2725});
2826```
2927
30- You'll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag:
28+ 你通常会在全局库的文档中看到如何在 HTML 脚本标签中使用库的指导:
3129
3230``` html
3331<script src =" http://a.great.cdn.for/someLib.js" ></script >
3432```
3533
36- Today, most popular globally-accessible libraries are actually written as UMD libraries (see below).
37- UMD library documentation is hard to distinguish from global library documentation.
38- Before writing a global declaration file, make sure the library isn't actually UMD.
34+ 如今,大多数流行的全局访问库实际上是以 UMD 库的形式编写的(请参见下文)。UMD 库文档很难与全局库文档区分开来。在编写全局声明文件之前,请确保库实际上不是 UMD。
3935
40- ## Identifying a Global Library from Code
36+ ## 从代码中识别全局库
4137
42- Global library code is usually extremely simple.
43- A global "Hello, world" library might look like this:
38+ 全局库代码通常非常简单。一个全局的 "Hello, world" 库可能如下所示:
4439
4540``` js
4641function createGreeting (s ) {
47- return " Hello, " + s;
42+ return " 你好, " + s;
4843}
4944```
5045
51- or like this:
46+ 或者像这样:
5247
5348``` js
5449window .createGreeting = function (s ) {
55- return " Hello, " + s;
50+ return " 你好, " + s;
5651};
5752```
5853
59- When looking at the code of a global library, you'll usually see:
54+ 在查看全局库的代码时,你通常会看到:
6055
61- - Top-level ` var ` statements or ` function ` declarations
62- - One or more assignments to ` window.someName `
63- - Assumptions that DOM primitives like ` document ` or ` window ` exist
56+ - 顶层的 ` var ` 语句或 ` function ` 声明
57+ - 一个或多个对 ` window.someName ` 的赋值
58+ - 假设 DOM 原语如 ` document ` 或 ` window ` 存在
6459
65- You _ won't _ see:
60+ 你 * 不会 * 看到:
6661
67- - Checks for, or usage of, module loaders like ` require ` or ` define `
68- - CommonJS/Node.js-style imports of the form ` var fs = require("fs"); `
69- - Calls to ` define(...) `
70- - Documentation describing how to ` require ` or import the library
62+ - 检查或使用像 ` require ` 或 ` define ` 这样的模块加载器
63+ - CommonJS/Node.js 风格的导入形式,如 ` var fs = require("fs"); `
64+ - 调用 ` define(...) `
65+ - 描述如何 ` require ` 或导入库的文档
7166
72- ## Examples of Global Libraries
67+ ## 全局库示例
7368
74- Because it's usually easy to turn a global library into a UMD library, very few popular libraries are still written in the global style.
75- However, libraries that are small and require the DOM (or have _ no_ dependencies) may still be global.
76-
77- ## Global Library Template
78-
79- You can see an example DTS below:
80-
81- ``` ts
82- // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
83- // Project: [~THE PROJECT NAME~]
84- // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
85-
86- /* ~ If this library is callable (e.g. can be invoked as myLib(3)),
87- *~ include those call signatures here.
88- *~ Otherwise, delete this section.
89- */
90- declare function myLib(a : string ): string ;
91- declare function myLib(a : number ): number ;
92-
93- /* ~ If you want the name of this library to be a valid type name,
94- *~ you can do so here.
95- *~
96- *~ For example, this allows us to write 'var x: myLib';
97- *~ Be sure this actually makes sense! If it doesn't, just
98- *~ delete this declaration and add types inside the namespace below.
99- */
100- interface myLib {
101- name: string ;
102- length: number ;
103- extras? : string [];
104- }
105-
106- /* ~ If your library has properties exposed on a global variable,
107- *~ place them here.
108- *~ You should also place types (interfaces and type alias) here.
109- */
110- declare namespace myLib {
111- // ~ We can write 'myLib.timeout = 50;'
112- let timeout: number ;
113-
114- // ~ We can access 'myLib.version', but not change it
115- const version: string ;
116-
117- // ~ There's some class we can create via 'let c = new myLib.Cat(42)'
118- // ~ Or reference e.g. 'function f(c: myLib.Cat) { ... }
119- class Cat {
120- constructor (n : number );
121-
122- // ~ We can read 'c.age' from a 'Cat' instance
123- readonly age: number ;
124-
125- // ~ We can invoke 'c.purr()' from a 'Cat' instance
126- purr(): void ;
127- }
128-
129- // ~ We can declare a variable as
130- // ~ 'var s: myLib.CatSettings = { weight: 5, name: "Maru" };'
131- interface CatSettings {
132- weight: number ;
133- name: string ;
134- tailLength? : number ;
135- }
136-
137- // ~ We can write 'const v: myLib.VetID = 42;'
138- // ~ or 'const v: myLib.VetID = "bob";'
139- type VetID = string | number ;
140-
141- // ~ We can invoke 'myLib.checkCat(c)' or 'myLib.checkCat(c, v);'
142- function checkCat(c : Cat , s ? : VetID );
143- }
144- ```
69+ 因为通常很容易将全局库转换为 UMD 库,所以很少有流行的库仍然以全局样式编写。但是,需要 DOM(或没有依赖性)的小型库可能仍然是全局的。
0 commit comments