将带有子函数的WinJS函数定义转换为TypeScript

时间:2013-10-02 13:52:43

标签: typescript

我一直在尝试为优秀的DefinitelyTyped TypeScript存储库做贡献。

我在WinJS中遇到了一个不寻常的函数声明,并想知道该函数最简洁的TypeScript定义是什么,因此编译器不会抱怨并且Visual Studio Intellisense正常工作。

我不知道如何翻译成TypeScript定义/存根的方法是render.valueMSDN):

template.render.value(href, dataContext, container)

大多数函数都很容易翻译,但函数函数value,我不知道如何干净/正确地表示。

到目前为止,我已经为Template班级(MSDN)做了这个,我只想让它完整:

class Template {
    public element: HTMLElement;
    public extractChild: boolean;
    public processTimeout: number;
    public debugBreakOnRender: boolean;
    public disableOptimizedProcessing: boolean;
    public isDeclarativeControlContainer: boolean;
    public bindingInitializer: any;

    constructor(element: HTMLElement, options?: any);
    public render(dataContext: any, container?: HTMLElement): WinJS.Promise<any>;
    public renderItem(item: any, recycled?: HTMLElement);
    // public render.value(  ***TODO 
}

1 个答案:

答案 0 :(得分:2)

这就是我想出来的。

declare class Template {
    element: HTMLElement;
    extractChild: boolean;
    processTimeout: number;
    debugBreakOnRender: boolean;
    disableOptimizedProcessing: boolean;
    isDeclarativeControlContainer: boolean;
    bindingInitializer: any;

    constructor(element: HTMLElement, options?: any);
    render: {
        (dataContext: any, container?: HTMLElement): WinJS.Promise<HTMLElement>;
        value(href: string, dataContext: any, container?: HTMLElement): WinJS.Promise<HTMLElement>;
    };
    renderItem(item: any, recycled?: HTMLElement);
}

我对从render返回的WinJS.Promise对象的理解是,它包含了作为container或新div传入的HTMLElement。这就是为什么我输入了承诺WinJS.Promise<HTMLElement>

对于render我只是简单介绍了类型,而不是给它起一个名字,并在其他地方宣布它,因为我觉得它更整洁。其定义中的第一行表示当您将其视为函数时会发生什么,而第二行只是该对象的常规成员。