typescript type for function arguments code example
Example 1: typescript function as parameter
function createPerson(name: string, doAction: () => void): void {
console.log(`Hi, my name is ${name}.`);
doAction(); // doAction as a function parameter.
}
// Hi, my name is Bob.
// performs doAction which is waveHands function.
createPerson('Bob', waveHands());
Example 2: typescript set argument type
// non typed function
function add(x, y) {
return x + y;
}
// typed function
function add(x: number, y: number): number {
return x + y;
}
// types can be found here: https://www.typescriptlang.org/docs/handbook/basic-types.html