全局 .d.ts

全局库

全局库是可以从全局范围访问的库(即不使用任何形式的 import)。许多库只是公开一个或多个全局变量以供使用。例如,如果您使用 jQuery,则可以通过简单地引用 $ 变量来使用它:

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

您通常会在全局库的文档中看到有关如何在 HTML 脚本标记中使用该库的指南:

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

今天,大多数流行的全球可访问库实际上都是作为 UMD 库编写的(见下文)。UMD 库文档很难与全局库文档区分开来。在编写全局声明文件之前,请确保该库实际上不是 UMD。

从代码中识别全局库

全局库代码通常非常简单。全局 "Hello, world" 库可能如下所示:

function createGreeting(s) {
  return "Hello, " + s;
}

或者像这样:

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

dUeBgdPv0i9QXUV+Ac7779fQCCf6N+PgHWffTEo7W0/WCx/l1b3b4uQr3N974wJUMLyyGJh5rxNLJOo8/s5DUQ==

    30OsVtCg17Eg6gyx2Rt5CH29o6Jce2IlF7s89k2LfCzMNlVoStafmtBAeOhjhdOgh6M2C8meW3JHN6Lr/KNXHmkyQ9K209fCdL7+7LZdVhAH9zEacNNhqg8cL25UUduxJOHc4VWxY+QOWTBQmWAiOB1o5f1eVfjzdj088fJmxj4rdLv/uUfL7tXPiuGROWuLpplTXdDX7Iaro5stWs09yU8RaUKso3oI5qLyeQOWilQ75mk2Ws3vlS+1YAIn9/XFss32JpTuyT7sK2yr9DBMWGTfUFC0FCvvHq0jpKVHgRyr29/K60eLvH5btUWpotRA

Z2bJwNyTyotT0qt6sPgNAuv91nSSFIDGmnDzzZIYtgg=

    QDivPV7wcrcTbBCYAbEiEDLT22Sgojh5xccAHhV0bd18XIcpfwiW+1sntsskYoGjz525B7mupgmMMNQR+wIK8EsAs07/CP35aBG31fzXaNec0ofkzlf6nbEGvai45dHjFlAAI6jG+KkypfsFw/PsShbOcb8Ft9TeGaah5L76W5Bxr2UrT5/hEQjzoMquzL135uqMiXNA/vSLM+y2U4iTv54XE7AY+dImJMfWxgjlJiMXRVrmFsEQaax4WnVxz3U7pYFSbuAioJBhNNNxwo1KwNLWNk28xFs1/7bB6ovZvX1gQZaDEDe1tEK0WbAB7UMSz5/5iemd8A4xezJWKgCIIdz1IrEF39wXHPjdEGLKH2A8fkLnSNGA8ER11ADY75GM

全局库的示例

g8Ul5VpwB/vAVQCbT3k47bKOYWO22TAJfoeVsYgMDPtqhWEoQhXRCHoDDgZasyqOu03iGT/IRjV0zN7KJkRVjeBhvU1y42plExVtHFsJOXW3HUoDLSd5kY4pJh57oOUKNplJ5yNxvKTyiUHpURzWsp8f+BhwxjoTpofDVdL6/DJ3eosbL1n7Kkv5+RCJN63QRBQxv5PAUyCiT+scoV943c24dYOUN/jJhyAF8ShUFQ6ZHB1JhUdsfZ9Ijrx7UO9Ws5gbgckTOJ/qvF1DmU8scQ==

全局库模板

9+oOIOlBCYWXNnCRJ6PxiAMFFidwH1L5y0q4ayzF/PJeY2G1qluLY7i6d6x2Z7Rs

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>

/*~ If this library is callable (e.g. can be invoked as myLib(3)),
 *~ include those call signatures here.
 *~ Otherwise, delete this section.
 */
declare function myLib(a: string): string;
declare function myLib(a: number): number;

/*~ If you want the name of this library to be a valid type name,
 *~ you can do so here.
 *~
 *~ For example, this allows us to write 'var x: myLib';
 *~ Be sure this actually makes sense! If it doesn't, just
 *~ delete this declaration and add types inside the namespace below.
 */
interface myLib {
  name: string;
  length: number;
  extras?: string[];
}

/*~ If your library has properties exposed on a global variable,
 *~ place them here.
 *~ You should also place types (interfaces and type alias) here.
 */
declare namespace myLib {
  //~ We can write 'myLib.timeout = 50;'
  let timeout: number;

  //~ We can access 'myLib.version', but not change it
  const version: string;

  //~ There's some class we can create via 'let c = new myLib.Cat(42)'
  //~ Or reference e.g. 'function f(c: myLib.Cat) { ... }
  class Cat {
    constructor(n: number);

    //~ We can read 'c.age' from a 'Cat' instance
    readonly age: number;

    //~ We can invoke 'c.purr()' from a 'Cat' instance
    purr(): void;
  }

  //~ We can declare a variable as
  //~   'var s: myLib.CatSettings = { weight: 5, name: "Maru" };'
  interface CatSettings {
    weight: number;
    name: string;
    tailLength?: number;
  }

  //~ We can write 'const v: myLib.VetID = 42;'
  //~  or 'const v: myLib.VetID = "bob";'
  type VetID = string | number;

  //~ We can invoke 'myLib.checkCat(c)' or 'myLib.checkCat(c, v);'
  function checkCat(c: Cat, s?: VetID);
}