typescript optional parameter code example
Example 1: typescript optional parameters
sayHello(hello?: string) {
console.log(hello);
}
sayHello();
sayHello('world');
Example 2: how to define optional parameter in typescript
function functionName(par1: number, par2?: number) {
}
Example 3: typescript default parameter
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 4: typescript optional parameters
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 5: how to make a parameter optional in typescript
function foo(x?: number) {
console.log("x : "+ x);
}
foo();
foo(6);
Example 6: typescript function as parameter
function createPerson(name: string, doAction: () => void): void {
console.log(`Hi, my name is ${name}.`);
doAction();
}
createPerson('Bob', waveHands());