TypeScript Type Guards
function formatValue(value: string | number): string {
if (typeof value === "string") {
return value.trim();
}
return value.toFixed(2);
}
console.log(formatValue(" hi ")); // "hi"
console.log(formatValue(3.14159)); // "3.14"