问题
I've recently discovered the typeof keyword in typescript and it almost saves the day. I want to describe a dojo ContentPane as an interface:
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;
}
But unfortunately I cannot do a typeof on an interface. I must instead describe ContentPane as a class but since typescript doesn't implement multiple inheritence (via extends) I must do this?
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;
}
Is there an alternative which does not require me to duplicate the signature of dijit._Container? Is there an explanation as to why typeof SomeInterface does not work?
回答1:
It is not yet possible to do this (0.9.5). See workitem.
回答2:
Interfaces in typescript can have call signatures. e.g. the following will compile:
interface Foo{
new (arg:string):number;
}
var SomeClass:Foo;
var someInstance = new SomeClass("somestring");
So you should not need typeof
with an interface and should simply do:
declare module dijit {
module layout {
interface ContentPane extends dijit._Widget, dijit._Container {
}
}
}
declare module "dijit/layout/ContentPane"
{
var ContentPane: dijit.layout.ContentPane;
export = ContentPane;
}
来源:https://stackoverflow.com/questions/21031912/typescript-typeof-on-an-interface