【问题标题】:Typescript definition for a constructor on a static class静态类上的构造函数的打字稿定义
【发布时间】:2017-10-21 08:41:12
【问题描述】:

我在弄清楚如何为以下代码编写类型定义时遇到了一些麻烦。我想描述它的最佳方式是包含命名构造函数的静态类。

var ajaxAdapter = new IAjaxAdapter.implement({
   ajax: function (work: IAjaxRequest) {
      ...
   }
}

但我收到了构建错误Property 'implement' does not exist on type 'typeof IAjaxAdapter'

我的 d.ts 现在看起来像这样,但显然有点不对

export class IAjaxAdapterInstance {
    constructor(any: any);
    ajax: (request: IAjaxRequest) => void;
}
export class IAjaxAdapter {
    implement: IAjaxAdapterInstance;
}

我不确定这是否可能,如果不是,我想我可以输入任何类型的...但我希望有所收获。

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    只看类型,因为您使用 implement 作为引用类而不是对象的静态属性,如果您添加 statictypeof,它应该类型:

    export class IAjaxAdapter {
        static implement: typeof IAjaxAdapterInstance;
    }
    

    【讨论】:

    • 效果很好!另外,我不知道您可以在定义文件中使用 static 和 typeof,学习!
    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多