typescript optional ? 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: typescript interface
interface LabeledValue {
label: string;
}
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);Try