Table of contents
Function
Typescript from v1.4 has the type keyword which declares a type alias (analogous to a typedef in C/C++).
You can declare your callback type thus:
type CallbackFunction = () => void;
declares a function that takes no arguments and returns nothing. A function that takes zero or more arguments of any type and returns nothing
would be:type CallbackFunctionVariadic = (...args: any[]) => void;
let callback: CallbackFunctionVariadic = function (...args: any[]) {
// do some stuff
};
If you want a function that takes an arbitrary number of arguments and returns anything (including void):
type CallbackFunctionVariadicAnyReturn = (...args: any[]) => any;
You can specify some mandatory arguments and then a set of additional arguments (say a string, a number and then a set of extra args) thus:
type CallbackFunctionSomeVariadic = ( arg1: string, arg2: number, ...args: any[] ) => void;
This can be useful for things like EventEmitter handlers.
Functions can be typed as strongly as you like in this fashion, although you can get carried away and run into combinatoric problems if you try to
nail everything down with a type alias.