TypeScript Operator Type Usage
1. Union Type
function printValue(value: any): void { if (typeof value === 'number') { console.log(`The value is a number: ${value}`); } else { console.log(`The value is a string: ${value}`); } } printValue(10); // The value is a number: 10 printValue('hello'); // The value is a string: hellofunction printValue(value: number | string): void { if (typeof value === 'number') { console.log(`The value is a number: ${value}`); } else { console.log(`The value is a string: ${value}`); } } printValue(10); // The value is a number: 10 printValue('hello'); // The value is a string: hello
(1) Advantages of Union Type
(2) Points to Note When Using Union Type
2. Intersection Type
Last updated