Challenge - 5 Problems
Discriminated Union Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of a simple discriminated union function
What is the output of the following TypeScript code?
Typescript
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
}
}
console.log(area({ kind: "circle", radius: 3 }));Attempts:
2 left
π‘ Hint
Recall the formula for the area of a circle is Ο Γ radiusΒ².
β Incorrect
The function calculates the area based on the shape kind. For a circle with radius 3, area = Ο Γ 3Β² = 28.2743 approximately.
π§ Conceptual
intermediateDiscriminated union type narrowing
Given the discriminated union below, which statement correctly narrows the type inside the if block?
Typescript
type Pet =
| { type: "dog"; barkVolume: number }
| { type: "cat"; livesLeft: number };
function speak(pet: Pet) {
if (/* which condition? */) {
console.log(pet.barkVolume);
}
}Attempts:
2 left
π‘ Hint
Discriminated unions use a common literal property to narrow types.
β Incorrect
Checking pet.type === "dog" narrows the type to the dog variant, so pet.barkVolume is accessible safely.
π§ Debug
advancedIdentify the error in discriminated union switch
What error will this TypeScript code produce?
Typescript
type Vehicle =
| { kind: "car"; wheels: 4 }
| { kind: "bike"; wheels: 2 };
function describe(vehicle: Vehicle) {
switch (vehicle.kind) {
case "car":
return `Car with ${vehicle.wheels} wheels`;
case "bike":
return `Bike with ${vehicle.wheels} wheels`;
case "truck":
return "Truck";
}
}Attempts:
2 left
π‘ Hint
Check the switch cases against the union type's possible 'kind' values.
β Incorrect
The 'truck' case is invalid because 'truck' is not part of the Vehicle union's 'kind' property. TypeScript flags this as an error.
π Syntax
advancedCorrect syntax for discriminated union with literal types
Which option correctly defines a discriminated union type for a shape with 'kind' as the discriminator?
Attempts:
2 left
π‘ Hint
Discriminated unions require a common property with distinct literal values.
β Incorrect
Option B uses distinct literal types for 'kind' which allows TypeScript to discriminate between variants.
π Application
expertDetermine the output of a complex discriminated union function
What is the output of this TypeScript code?
Typescript
type Event =
| { type: "click"; x: number; y: number }
| { type: "keypress"; key: string };
function handleEvent(e: Event): string {
switch (e.type) {
case "click":
return `Clicked at (${e.x},${e.y})`;
case "keypress":
return `Key pressed: ${e.key}`;
}
}
console.log(handleEvent({ type: "keypress", key: "Enter" }));Attempts:
2 left
π‘ Hint
Check which case matches the event type and what properties are accessed.
β Incorrect
The event type is 'keypress', so the function returns 'Key pressed: Enter'.
