Currently I have type definition as:
interface Param { title: string; callback: any; }
I need something like:
interface Param { title: string; callback: function; }
but the 2nd one is not being accepted.
Use Type Keyword to Declare Callback Type in TypeScript So with the help of the type keyword, you could declare your callback type as shown below. Copy type callBackFunction = () => void; This declares a function that does not take any arguments and returns nothing.
A function's type can be defined with (arg0: type0) => returnType and we can use this type definition in another function's parameter list.
Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.
The global type Function
serves this purpose.
Additionally, if you intend to invoke this callback with 0 arguments and will ignore its return value, the type () => void
matches all functions taking no arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With