ic3 ic3 - 2 months ago 15
TypeScript Question

Anonymous class in Typescript

Getting some trouble to do the following in typescript. I've the following interface defined :

interface RendererCallback {

getNode():HTMLElement;

getTable();
}


There is a method expecting a RenderCallback object like :

render( callback:RendererCallback )


How do I create an anonymous instance in the middle of my code :

myObject.render( new .. {
getNode() {
return myNode;
}
getTable() {
.. some code...
return something;
}
} );

Answer

You can use anonymous objects and lambdas:

myObject.render({
    getNode: () => { doSomething...; return myNode },
    getTable: () => myTable
});

Note, the new keyword is not used.

Comments