typescript arrow function as parameter type code example
Example 1: generic arrow function typescript
const foo = <T extends unknown>(x: T) => x;
Example 2: typescript arrow function
let sum = (x: number, y: number): number => {
return x + y;
}
sum(10, 20);
Example 3: typescript pass a function as an argunetn
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback);
foo.save(numCallback);
Example 4: arrow function in ts
var getResult = (username: string, points: number): string => {
return `${ username } scored ${ points } points!`;
};
getResult('joyous jackal' , 100);