我最近在打字稿中发现了typeof关键字,它几乎节省了一天。我想将dojo ContentPane描述为一个接口:
declare module dijit {
module layout {
interface ContentPane extends dijit._Widget, dijit._Container {
}
}
}
declare module "dijit/layout/ContentPane"
{
var ContentPane: typeof dijit.layout.ContentPane;
export = ContentPane;
}
但不幸的是我不能在界面上做一个typeof。我必须将ContentPane描述为一个类,但由于typescript没有实现多重继承(通过extends),我必须这样做吗?
declare module dijit {
module layout
{
class ContentPane extends dijit._Widget
implements dijit._Container
{
// how to avoid duplicating the _Container here?
addChild(widget: _WidgetBase, insertIndex?: number): void;
getIndexOfChild(child: _WidgetBase): number;
hasChildren(): boolean;
removeChild(widget: _WidgetBase): void;
removeChild(widget: number): void;
}
}
}
declare module "dijit/layout/ContentPane"
{
var ContentPane: typeof dijit.layout.ContentPane;
export = ContentPane;
}
是否有替代方案不要求我复制dijit._Container的签名?有没有解释为什么someInterface的类型不起作用?
答案 0 :(得分:1)
目前尚无法做到这一点(0.9.5)。请参阅workitem。
答案 1 :(得分:0)
打字稿中的接口可以有呼叫签名。例如以下将编译:
interface Foo{
new (arg:string):number;
}
var SomeClass:Foo;
var someInstance = new SomeClass("somestring");
所以你不需要typeof
一个接口,而应该只做:
declare module dijit {
module layout {
interface ContentPane extends dijit._Widget, dijit._Container {
}
}
}
declare module "dijit/layout/ContentPane"
{
var ContentPane: dijit.layout.ContentPane;
export = ContentPane;
}