【问题标题】:How to overwrite DefinitelyTyped or reference a custom file如何覆盖肯定类型或引用自定义文件
【发布时间】:2019-04-03 10:36:39
【问题描述】:

我正在使用 Node 库 (https://github.com/lbdremy/solr-node-client),在使用 VSCode 时,它​​会自动引入社区编写的类型定义 (https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/solr-client)。

我的问题是缺少方法,或者我更喜欢让某些方法返回更精确的类型(特定于我的项目)。

对于我自己的代码,我可以使用

/** 
 * @typedef {Number} CustomType
 */

问题

如何创建单独的文件并添加/修改现有定义的类型。

【问题讨论】:

    标签: node.js typescript visual-studio-code


    【解决方案1】:

    根据类型的声明方式(命名空间或模块),您可以创建一个类型定义文件 *.d.ts 并在其中添加要合并的声明。

    我有一个玩笑匹配器的例子:

    declare namespace jest {
      interface Matchers<R> {
        toHave(expected: string): R
      }
    }
    
    

    确保您的定义文件包含在您的 tsconfig 正在查看的文件中。

    https://www.typescriptlang.org/docs/handbook/declaration-merging.html

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2012-11-15
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 2014-02-23
      • 2013-12-28
      相关资源
      最近更新 更多