0

I've been trying to contribute to excellent DefinitelyTyped repository of TypeScript.

I encountered an unusual function declaration in WinJS though and wondered what the most succinct TypeScript definition would be for the function, so that the compiler won't complain and Visual Studio Intellisense works correctly.

The method I don't know how to translate to a TypeScript definition/stub is render.value (MSDN):

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

Most functions are easy to translate, but the function on the function, value, I don't know how to represent cleanly/correctly.

So far, I've got this for the Template class (MSDN), I just want it to be complete:

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 
}
3
  • Here's my quick stab, not knowing anything about WinJS. It doesn't look like render should be a promise. value looks like a promise. render just looks like a property that has a value property that is a promise. Commented Oct 2, 2013 at 13:58
  • Also, you can't have a method named "render.value", so you'll have to split them up somehow. Commented Oct 2, 2013 at 13:59
  • render returns a Promise (I'm using it). I understand that functions can't be named that way. It's a function declared on a function in JavaScript. The WinJS library has the function. I didn't create it. Commented Oct 2, 2013 at 14:02

1 Answer 1

2

Here's what I came up with.

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);
}

My understanding of the WinJS.Promise object returned from render is that it's wrapping the HTMLElement passed in as container or a new div. So that's why I typed the promise WinJS.Promise<HTMLElement>.

For render I just inlined the type rather than giving it a name and declaring it elsewhere because I think that's neater. The first line inside its definition says what happens when you treat it like a function, and the second is just a regular member of that object.

Sign up to request clarification or add additional context in comments.

2 Comments

That seems to do the trick. I wasn't aware of the syntax for declaring the function on the render object directly.
Yeah it doesn't come up often.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.