Defining TypeScript callback type Defining TypeScript callback type typescript typescript

Defining TypeScript callback type


I just found something in the TypeScript language specification, it's fairly easy. I was pretty close.

the syntax is the following:

public myCallback: (name: type) => returntype;

In my example, it would be

class CallbackTest{    public myCallback: () => void;    public doWork(): void    {        //doing some work...        this.myCallback(); //calling callback    }}


To go one step further, you could declare a type pointer to a function signature like:

interface myCallbackType { (myArgument: string): void }

and use it like this:

public myCallback : myCallbackType;


You can declare a new type:

declare type MyHandler = (myArgument: string) => void;var handler: MyHandler;

Update.

The declare keyword is not necessary. It should be used in the .d.ts files or in similar cases.