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,$变量可以通过简单的引用来使用。
$(() => {
console.log("hello!");
});
You’ll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag:
你通常会在全局库的文档中看到关于如何在HTML脚本标签中使用该库的指导。
<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 “库可能看起来像这样。
function createGreeting(s) {
return "Hello, " + s;
}
or like this
window.createGreeting = function (s) {
return "Hello, " + s;
};
When looking at the code of a global library, you’ll usually see:
当查看一个全局库的代码时,你通常会看到。
Top-level
var
statements orfunction
declarations顶层的var语句或函数声明
One or more assignments to
window.someName
一个或多个对window.someName的赋值
Assumptions that DOM primitives like
document
orwindow
exist假设DOM基元如document或window的存在
You won’t see:你不会看到。
Checks for, or usage of, module loaders like
require
ordefine
对模块加载器的检查或使用,如require或define
CommonJS/Node.js-style imports of the form
var fs = require("fs");
CommonJS/Node.js风格的导入形式 var fs = require("fs")。
Calls to
define(...)
对define(...)的调用
Documentation describing how to
require
or import the library描述如何要求或导入该库的文档
全局库的例子
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的例子。
// 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);
}