Challenge - 5 Problems
Union Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this union type check?
Consider the following TypeScript code. What will be logged to the console?
Typescript
type Result = string | number; function printResult(value: Result) { if (typeof value === 'string') { console.log(value.toUpperCase()); } else { console.log(value + 10); } } printResult('hello'); printResult(5);
Attempts:
2 left
💡 Hint
Check how the typeof operator distinguishes between string and number.
✗ Incorrect
The function checks if the value is a string, then converts it to uppercase. Otherwise, it adds 10 to the number. So 'hello' becomes 'HELLO' and 5 becomes 15.
🧠 Conceptual
intermediate1:30remaining
Which union type allows both string and boolean values?
Which of the following TypeScript union types correctly allows a variable to hold either a string or a boolean?
Attempts:
2 left
💡 Hint
Union types use the pipe symbol | to combine types.
✗ Incorrect
The pipe symbol | creates a union type allowing either type. The ampersand & creates an intersection type, which requires both types simultaneously, which is impossible for string and boolean.
🔧 Debug
advanced2:00remaining
Why does this union type assignment cause an error?
Examine the code below. Why does the assignment cause a TypeScript error?
Typescript
type ID = number | string; let userId: ID = true;
Attempts:
2 left
💡 Hint
Check the allowed types in the union and the assigned value type.
✗ Incorrect
The union type ID allows only number or string. The boolean value 'true' is not assignable to either, so TypeScript raises an error.
❓ Predict Output
advanced2:30remaining
What is the output of this function with union types and type narrowing?
What will the following TypeScript code output when executed?
Typescript
type Input = string | number | boolean; function describe(input: Input) { switch (typeof input) { case 'string': return `String: ${input.length}`; case 'number': return `Number: ${input * 2}`; case 'boolean': return `Boolean: ${input ? 'yes' : 'no'}`; } } console.log(describe('abc')); console.log(describe(4)); console.log(describe(false));
Attempts:
2 left
💡 Hint
Look at how typeof narrows the type and what each case returns.
✗ Incorrect
For 'abc', length is 3. For 4, doubling gives 8. For false, ternary returns 'no'. The switch covers all types, so no runtime error.
🧠 Conceptual
expert3:00remaining
How does TypeScript handle union types with overlapping properties?
Given these interfaces and union type, what is the type of 'value.name' inside the function?
Typescript
interface A { name: string; age: number; }
interface B { name: string; location: string; }
type AB = A | B;
function printName(value: AB) {
return value.name;
}Attempts:
2 left
💡 Hint
Check which properties are common to both interfaces in the union.
✗ Incorrect
Both interfaces A and B have a 'name' property of type string. So in the union type AB, 'name' is guaranteed to be a string.