【问题标题】:TypeScript JQuery interface merging needs to reference moduleTypeScript JQuery 接口合并需要引用模块
【发布时间】:2018-04-16 20:34:16
【问题描述】:

我很难找到关于如何为我的模块布局 d.ts 文件的良好指导。

编译到目标 ES5 和 CommonJS 模块。

我有一个 remote.ts 文件,它同时导出类 Remote 和接口 IRemoteOptions。

//remote.ts
export class Remote {
 //...
}
export interface IRemoteOptions{
  ///...
}

然后我扩展 JQuery 以添加一个函数 remote(options?: IRemoteOptions)。

//global.d.ts
interface JQuery{
 remote(options? IRemoteOptions);
}

如何确保 global.d.ts 知道 IRemoteOptions 而不向该文件添加导入语句?如果我添加一个 import 语句,它本身就会成为一个模块并破坏扩展JQuery 的范围。

我们还有许多其他的 JQuery 扩展点需要添加,这样做的正确方法是什么?

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    我使用全局增强方法完成了这项工作。

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

    我将 JQuery 接口扩展移回了我的 remote.ts 文件,如下所示:

    //remote.ts
    export class Remote {
     //...
    }
    export interface IRemoteOptions{
      ///...
    }
    declare global{
      interface JQuery{
        remote(options? IRemoteOptions);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 2016-08-02
      • 1970-01-01
      • 2019-01-07
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      相关资源
      最近更新 更多