Determining TypeScript Callback Type

I have the following class in TypeScript:

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

I use the class as follows:

 var test = new CallbackTest(); test.myCallback = () => alert("done"); test.doWork(); 

The code works, so it displays a message box as expected.

My question is: is there any type that I can provide for my field of class myCallback ? The public field myCallback is now of type any , as shown above. How to determine the signature of a callback method? Or can I just set the type of the callback type? Or can I do it? Should I use any (implicit / explicit)?

I tried something like this, but this did not work (compile-time error):

 public myCallback: (); // or: public myCallback: function; 

I could not find any explanation for this online, so I hope you can help me.

+140
types callback typescript
Oct 30
source share
7 answers

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

The syntax is as follows:

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

In my example, it will be

 class CallbackTest { public myCallback: () => void; public doWork(): void { //doing some work... this.myCallback(); //calling callback } } 
+181
Oct 30 '12 at 10:51
source share

To take another step, you can declare a type pointer to a function signature, for example:

 interface myCallbackType { (myArgument: string): void } 

and use it as follows:

 public myCallback : myCallbackType; 
+128
Jan 10 '13 at 1:39
source share

You can declare a new type:

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

Update

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

+51
Oct. 16 '15 at 14:52
source share

Here is an example - do not accept any parameters and do not return anything.

 class CallbackTest { public myCallback: {(): void;}; public doWork(): void { //doing some work... this.myCallback(); //calling callback } } var test = new CallbackTest(); test.myCallback = () => alert("done"); test.doWork(); 

If you want to accept a parameter, you can also add this:

 public myCallback: {(msg: string): void;}; 

And if you want to return a value, you can add this as well:

 public myCallback: {(msg: string): number;}; 
+29
Oct 30
source share

If you want to use a generic function, you can use the following. Although, it seems, it is not documented anywhere.

 class CallbackTest { myCallback: Function; } 
+10
Jul 15 '16 at 17:40
source share

I encountered the same error while trying to add a callback to an event listener. Oddly enough, setting the EventListener callback type solved it. It looks more elegant than defining an entire function as a type, but I'm not sure if this is the right way to do this.

 class driving { // the answer from this post - this works // private callback: () => void; // this also works! private callback:EventListener; constructor(){ this.callback = () => this.startJump(); window.addEventListener("keydown", this.callback); } startJump():void { console.log("jump!"); window.removeEventListener("keydown", this.callback); } } 
+1
Apr 24 '17 at 9:12
source share

You can use the following:

  1. Enter Alias ​​(using the type keyword, a functional literal alias)
  2. interface function literal



Here is an example of how to use them:

 type myCallbackType = (arg1: string, arg2: boolean) => number; interface myCallbackInterface { (arg1: string, arg2: boolean): number }; class CallbackTest { // ... public myCallback2: myCallbackType; public myCallback3: myCallbackInterface; public myCallback1: (arg1: string, arg2: boolean) => number; // ... } 
0
Aug 07 '19 at 13:50
source share



All Articles