EasyType Inference💻 Output Question

typeof narrowing determines which branch runs

💡

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.

What does this output?

// 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);

Correct Output

string:HELLO
number:3.1
boolean:true
other

Why this output?

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.

More Type Inference Output Questions

MediumConst assertion — literal type vs widened typeMediumDiscriminated union narrowing via switchHardReturnType inference — function return shapes

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz