Anonymous class in Typescript Anonymous class in Typescript typescript typescript

Anonymous class in Typescript


You can use anonymous objects and lambdas:

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

Note, the new keyword is not used.


Typescript now supports class expressions so you can make actual anonymous classes.

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

This has the benefit of this not being inferred to be any within the anonymous class' methods.


Here's a full code sample that works:

 interface RendererCallback {        getNode():HTMLElement;        getTable(); } class thing {     render(callback: RendererCallback) {     } } var myObject = new thing(); myObject.render(  {    getNode() {        return null;    },    getTable() {    }    }  );