打字稿:如何在全局声明的命名空间中扩展现有接口 [英] Typescript: How to extend an existing interface in a globally declared namespace
问题描述
我正在尝试扩展KendoUI的现有接口,该接口来自定义文件. 通过接口合并,这通常很简单,但是我要扩展的接口位于特定的全局命名空间"kendo.ui"中.
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"
我必须尝试像这样将"hideInEditor"属性添加到kendo.ui.GridColumn接口的接口.
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;
}
}
但是,编译器似乎已经忘记了所有原始定义,并且由于缺少所有类型,我无法再访问kendo命名空间.因此,显然这是错误的.但是扩展这种接口的正确方法是什么?
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?
推荐答案
将类型定义文件放在同一文件夹中不会执行任何操作,除非您以某种方式告诉编译器寻找它们.
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
请注意第一行带有reference
.
这篇关于打字稿:如何在全局声明的命名空间中扩展现有接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!