Typescript: How to extend an existing interface in a globally declared namespace

☆樱花仙子☆ 提交于 2019-12-11 08:28:23

问题


I'm trying to extend an existing interface of KendoUI which comes from a definition file. With interface merging this is normally straight forward but the interface I'm going to extend is in a specific global namespace "kendo.ui"

I'm ust trying to add "hideInEditor" propterty to the interface to the kendo.ui.GridColumn interface like this.

namespace kendo.ui {
    export interface GridColumn {
        hideInEditor?: boolean;
    }   
}

However, the compiler seems to have forgotten all about the orginal definitions and I can no longer access the kendo namespace as all types are missing. So apparently this is wrong. But what is the correct way to extend such an interface?


回答1:


Having the type definition files in the same folder doesn't do anything unless you somehow tell the compiler to look for them.

In your case I think you need this:

/// <reference path="./kendo.ui.d.ts" />

namespace kendo.ui {
    export interface GridColumn {
        hideInEditor?: boolean;
    }
}

let obj: kendo.ui.GridColumn;
console.log(kendo.culture()); // ok
console.log(obj.format); // ok
console.log(obj.hideInEditor); // ok

Note the first line with the reference.



来源:https://stackoverflow.com/questions/41101727/typescript-how-to-extend-an-existing-interface-in-a-globally-declared-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!