function example in typescript
Example 1: typescript default parameter
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 2: typescript function
function greet(name: string): string {
return name.toUpperCase();
}
console.log(greet("hello"));
console.log(greet(1));
Example 3: typescript function as parameter
function createPerson(name: string, doAction: () => void): void {
console.log(`Hi, my name is ${name}.`);
doAction();
}
createPerson('Bob', waveHands());
Example 4: typescript function type
interface Date {
toString(): string;
setTime(time: number): number;
}
Example 5: types function typescript
interface Param {
title: string;
callback: function;
}