https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-d-ts.html

Global Libraries全局库

A global library is one that can be accessed from the global scope (i.e. without using any form of import). Many libraries simply expose one or more global variables for use. For example, if you were using jQuery, the $ variable can be used by simply referring to it:
全局库是一个可以从全局范围访问的库(即不使用任何形式的导入)。许多库只是暴露了一个或多个全局变量以供使用。例如,如果你使用的是jQuery,$变量可以通过简单的引用来使用。

  1. $(() => {
  2. console.log("hello!");
  3. });

You’ll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag:
你通常会在全局库的文档中看到关于如何在HTML脚本标签中使用该库的指导。

  1. <script src="http://a.great.cdn.for/someLib.js"></script>

Today, most popular globally-accessible libraries are actually written as UMD libraries (see below). UMD library documentation is hard to distinguish from global library documentation. Before writing a global declaration file, make sure the library isn’t actually UMD.
今天,大多数流行的全局访问库实际上都写成了UMD库(见下文)。UMD库的文档与全局库的文档很难区分。
在编写全局声明文件之前,要确保该库实际上不是UMD库。

从代码中识别一个全局库

Identifying a Global Library from Code

Global library code is usually extremely simple. A global “Hello, world” library might look like this:
全局库的代码通常是非常简单的。一个全局的 “Hello, world “库可能看起来像这样。

  1. function createGreeting(s) {
  2. return "Hello, " + s;
  3. }

or like this

  1. window.createGreeting = function (s) {
  2. return "Hello, " + s;
  3. };

When looking at the code of a global library, you’ll usually see:
当查看一个全局库的代码时,你通常会看到。

  • Top-level var statements or function declarations

    1. 顶层的var语句或函数声明
  • One or more assignments to window.someName

    1. 一个或多个对window.someName的赋值
  • Assumptions that DOM primitives like document or window exist

    1. 假设DOM基元如documentwindow的存在

You won’t see:你不会看到。

  • Checks for, or usage of, module loaders like require or define

    1. 对模块加载器的检查或使用,如requiredefine
  • CommonJS/Node.js-style imports of the form var fs = require("fs");

    1. CommonJS/Node.js风格的导入形式 var fs = require("fs")。
  • Calls to define(...)

    1. define(...)的调用
  • Documentation describing how to require or import the library

    1. 描述如何要求或导入该库的文档

全局库的例子

Examples of Global Libraries

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. However, libraries that are small and require the DOM (or have no dependencies) may still be global.
因为通常很容易将全局库变成UMD库,所以很少有流行的库仍然以全局风格编写。然而,那些小的、需要DOM的(或没有依赖关系的)库仍然可以是全局的。

全局库模板

Global Library Template

You can see an example DTS below:
你可以在下面看到一个DTS的例子。

  1. // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
  2. // Project: [~THE PROJECT NAME~]
  3. // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
  4. /*~ If this library is callable (e.g. can be invoked as myLib(3)),
  5. *~ include those call signatures here.
  6. *~ Otherwise, delete this section.
  7. */
  8. declare function myLib(a: string): string;
  9. declare function myLib(a: number): number;
  10. /*~ If you want the name of this library to be a valid type name,
  11. *~ you can do so here.
  12. *~
  13. *~ For example, this allows us to write 'var x: myLib';
  14. *~ Be sure this actually makes sense! If it doesn't, just
  15. *~ delete this declaration and add types inside the namespace below.
  16. */
  17. interface myLib {
  18. name: string;
  19. length: number;
  20. extras?: string[];
  21. }
  22. /*~ If your library has properties exposed on a global variable,
  23. *~ place them here.
  24. *~ You should also place types (interfaces and type alias) here.
  25. */
  26. declare namespace myLib {
  27. //~ We can write 'myLib.timeout = 50;'
  28. let timeout: number;
  29. //~ We can access 'myLib.version', but not change it
  30. const version: string;
  31. //~ There's some class we can create via 'let c = new myLib.Cat(42)'
  32. //~ Or reference e.g. 'function f(c: myLib.Cat) { ... }
  33. class Cat {
  34. constructor(n: number);
  35. //~ We can read 'c.age' from a 'Cat' instance
  36. readonly age: number;
  37. //~ We can invoke 'c.purr()' from a 'Cat' instance
  38. purr(): void;
  39. }
  40. //~ We can declare a variable as
  41. //~ 'var s: myLib.CatSettings = { weight: 5, name: "Maru" };'
  42. interface CatSettings {
  43. weight: number;
  44. name: string;
  45. tailLength?: number;
  46. }
  47. //~ We can write 'const v: myLib.VetID = 42;'
  48. //~ or 'const v: myLib.VetID = "bob";'
  49. type VetID = string | number;
  50. //~ We can invoke 'myLib.checkCat(c)' or 'myLib.checkCat(c, v);'
  51. function checkCat(c: Cat, s?: VetID);
  52. }