Hint
TypeScript uses typeof narrowing to refine union types in conditional blocks. After typeof val === "string", TypeScript knows val is string and allows string methods.
// Simulate TypeScript's typeof narrowing at runtime
function describe(val) {
if (typeof val === 'string') {
console.log('string:' + val.toUpperCase());
} else if (typeof val === 'number') {
console.log('number:' + val.toFixed(1));
} else if (typeof val === 'boolean') {
console.log('boolean:' + val);
} else {
console.log('other');
}
}
describe('hello');
describe(3.14159);
describe(true);
describe(null);string:HELLO number:3.1 boolean:true other
Explanation: Each call narrows to the matching branch. null has typeof "object" so it falls through to the else branch.
Key Insight: TypeScript uses typeof narrowing to refine union types in conditional blocks. After typeof val === "string", TypeScript knows val is string and allows string methods.