Typescript typeof on an interface?

拜拜、爱过 提交于 2019-12-14 00:26:27

问题


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

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