typescript optional types code example
Example 1: typescript optional parameters
sayHello(hello?: string) {
console.log(hello);
}
sayHello();
sayHello('world');
Example 2: typescript optional parameters
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 3: use type as value typescript
Typescript interfaces aren''t being compiled into the js output, and you can not use them at runtime
Example 4: typescript make function argument optional
function multiply(a: number, b: number, c?: number): number {
if (typeof c !== 'undefined') {
return a * b * c;
}
return a * b;
}