function types typescript code example
Example 1: typescript default parameter
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 2: typescript function as parameter
function createPerson(name: string, doAction: () => void): void {
console.log(`Hi, my name is ${name}.`);
doAction();
}
createPerson('Bob', waveHands());
Example 3: typescript function type
function sayHello(name: string): string {
console.log(`Hello, ${name}`!);
}
sayHello('Bob');
Example 4: typescript function
function add(x: number, y: number): number {
return x + y;
}
let myAdd = function (x: number, y: number): number {
return x + y;
};
Example 5: types function typescript
interface Param {
title: string;
callback: function;
}
Example 6: types function typescript
interface Alternate_Syntax_4_Advanced {
title: string;
callback<T extends unknown[], R = unknown>(...args?: T): R;
}