【问题标题】:How do I extend a typings declaration?如何扩展类型声明?
【发布时间】:2017-02-12 04:30:11
【问题描述】:

我正在使用一个 npm 模块,该模块在 DefinitelyTyped/DefinitelyTyped github repo 中有声明,但该模块的类型不完整。

我已提出拉取请求以获取缺失的声明,但是在其被接受之前,我需要有一种方法来临时扩展不完整的声明。

理想情况下,我希望能够在我的项目根目录中有一个 temporaryTypings.d.ts 文件,我可以在其中为遇到的每种情况添加声明。

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    您可以从非 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 因为 typescript 2 也可以从特定的 git repo 安装模块。

    【讨论】:

    • 好主意,我将如何使用新的 npm @types 语法来做到这一点?
    • @nhjk @types/pkg 也是 npm 包。我想应该能够发布一个包含更新内容的非官方包,或者只使用带有npm-link 的本地 npm 包。
    【解决方案2】:

    您总是可以在源文件的某处有一些project.d.ts 文件。

    您可以从诸如/// <reference path="path/to/project.d.ts" /> 或您的tsconfig.json 之类的入口点引用它,然后在那里添加适合您的类型。

    例如,我的全局文件通常包含:

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

    这有助于我在没有require 的情况下导入我的模块。定义文件可用后,您可以使用npm install @types/leftpad --save-dev 安装它们,然后从global.d.ts 文件中删除该模块声明。

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2018-05-25
      相关资源
      最近更新 更多