Below is the example code from my textbook:
function calculateTax(amount: number, format: boolean): string | number {
const calcAmount = amount * 1.2;
return format ? `$${calcAmount.toFixed(2)}` : calcAmount;
}
let taxValue = calculateTax(100, false);
switch (typeof taxValue) {
case "number":
console.log(`Number Value: ${taxValue.toFixed(2)}`);
break;
case "string":
console.log(`String Value: ${taxValue.charAt(0)}`);
break;
default:
let value: never = taxValue; // taxValue's type is "never"
console.log(`Unexpected type for value: ${value}`);
}
and the author says:
TypeScript provides the never type to ensure you can’t accidentally use a value once type guards have been used to exhaustively narrow a value to all of its possible types
I'm confused, in the the default branch, I can assign the never type to any other types as:
default:
let value: number = taxValue; // or let value: number = taxValue;
console.log(`Unexpected type for value: ${value}`);
so what does the author mean by "can’t accidentally use a value"? and what's the purpose of never in this example, I can't see any benefits to use "never" type.