引用

下载

🌐 Downloading

除了 npm 之外,获取类型声明不需要任何工具。

🌐 Getting type declarations requires no tools apart from npm.

例如,获取像 lodash 这样的库的声明只需要以下命令

🌐 As an example, getting the declarations for a library like lodash takes nothing more than the following command

cmd
npm install --save-dev @types/lodash

值得注意的是,如果 npm 包已经包含了其声明文件,如 Publishing 中所述,则无需下载相应的 @types 包。

🌐 It is worth noting that if the npm package already includes its declaration file as described in Publishing, downloading the corresponding @types package is not needed.

引用

🌐 Consuming

从那里你就可以在 TypeScript 代码中轻松使用 lodash。
这适用于模块代码和全局代码。

🌐 From there you’ll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global code.

例如,一旦你对类型声明进行了 npm install 操作,就可以使用导入并编写

🌐 For example, once you’ve npm install-ed your type declarations, you can use imports and write

ts
import * as _ from "lodash";
_.padStart("Hello TypeScript!", 20, " ");

或者如果你没有使用模块,你可以直接使用全局变量 _

🌐 or if you’re not using modules, you can just use the global variable _.

ts
_.padStart("Hello TypeScript!", 20, " ");

搜索

🌐 Searching

在大多数情况下,类型声明包的名称应始终与 npm 上的包名称相同,但前面加上 @types/ 前缀。不过,如果需要,你可以使用 Yarn 包搜索 来查找你喜欢的库的包。

🌐 For the most part, type declaration packages should always have the same name as the package name on npm, but prefixed with @types/, but if you need, you can use the Yarn package search to find the package for your favorite library.

注意:如果你要查找的声明文件不存在,你可以随时贡献一个,为下一个寻找它的开发者提供帮助。 有关详细信息,请参阅 DefinitelyTyped 的贡献指南页面