Skip to main content
TypeScript Output Prediction Easy +10 XP

Union Type Check

What is the type of x?

TypeScriptRead-only
let x: string | number = "hello";
x = 42;
// typeof x at this point?

What is the output?

Show answer and explanation

Answer: number

TypeScript narrows the type based on assignment. After x = 42, TypeScript knows x is currently a number.

Hint: After the assignment, TypeScript knows the current type.

What this challenge tests

Output Prediction in TypeScript: union-types, type-narrowing.

Related challenges