下载
¥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 包已经包含 发布 中描述的声明文件,则不需要下载对应的 @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 贡献指南页面。
¥Note: if the declaration file you are searching for is not present, you can always contribute one back and help out the next developer looking for it. Please see the DefinitelyTyped contribution guidelines page for details.