简体   繁体   English

如何扩展类型声明?

[英]How do I extend a typings declaration?

I'm using an npm module that has declarations in the DefinitelyTyped/DefinitelyTyped github repo, but typings for the module are incomplete.我正在使用一个 npm 模块,它在绝对类型/明确类型的 github 存储库中有声明,但模块的类型不完整。

I have made a pull request to get the missing declarations in, however until its accepted I need to have a way to temporarily extend the incomplete declaration.我已经提出了一个拉取请求来获取丢失的声明,但是在它被接受之前,我需要有一种方法来临时扩展不完整的声明。

Ideally I would like to be able to have a temporaryTypings.d.ts file in my project root where I can add declarations for every situation like this that I run into.理想情况下,我希望能够在我的项目根目录中有一个temporaryTypings.d.ts文件,我可以在其中为遇到的每种情况添加声明。

You can install definitions from non-DefinitelyTyped source, like your fork of DT:您可以从非 DefinitelyTyped 源安装定义,例如您的 DT 分支:

# (this example is taken from https://www.npmjs.com/package/typings )
# If you need a specific commit from github. 
$ typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#1c05872e7811235f43780b8b596bfd26fe8e7760

npm since typescript 2 can install module from specific git repo as well. npm因为 typescript 2 也可以从特定的 git repo 安装模块。

You can always have some project.d.ts file somewhere in your source file.你总是可以在你的源文件中的某个地方有一些project.d.ts文件。

You can reference to it from some entry point like /// <reference path="path/to/project.d.ts" /> or from your tsconfig.json and just add the typings that are working for you there.您可以从某个入口点(如/// <reference path="path/to/project.d.ts" />或从您的tsconfig.json然后在那里添加适合您的类型。

For example my global file usually consists :例如,我的全局文件通常包括:

declare module "leftpad" {
    var t : any;
    export = t;
}

Which helps me to import my modules without require .这有助于我在没有require情况下导入我的模块。 After the definition files are available, you can install them with npm install @types/leftpad --save-dev and then just remove that module declaration from your global.d.ts file.定义文件可用后,您可以使用npm install @types/leftpad --save-dev安装它们,然后从global.d.ts文件中删除该模块声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM