Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a type for callback functions in TypeScript?

Tags:

typescript

enter image description here The error says Supplied parameter do not match any signature of call target. When I replace Function with any as the second parameter's type, the error disappears. But any is the same as no type, isn't there a suitable type for functions as parameters?

like image 246
jstice4all Avatar asked Feb 05 '15 13:02

jstice4all


People also ask

What is the type of callback in TypeScript?

What are Callback Functions in TypeScript. A callback function is defined as a function passed into another function as an argument, which is then invoked inside the outer function to complete the desirable routine or action.

What is the type of callback function?

There are 2 kinds of callback functions: synchronous and asynchronous. The synchronous callbacks are executed at the same time as the higher-order function that uses the callback. Synchronous callbacks are blocking.

Is there a function type in TypeScript?

Types of Functions in TypeScript: There are two types of functions in TypeScript: Named Function. Anonymous Function.


1 Answers

Instead of Function (or any) you can use the following type for your callback parameter:

(ev: Event)=> any

This matches the type expected by addEventListener.

Here is the full function signature:

on(eventName: string, callback: (ev: Event)=> any, useCapture: boolean) : Dom.Element {
    //...
like image 194
Fenton Avatar answered Sep 19 '22 08:09

Fenton