Challenge - 5 Problems
Intersection 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 TypeScript code using intersection types?
Consider the following TypeScript code. What will be logged to the console?
Typescript
type A = { x: number };
type B = { y: string };
const obj: A & B = { x: 10, y: "hello" };
console.log(obj.x, obj.y);Attempts:
2 left
💡 Hint
An intersection type combines all properties from both types.
✗ Incorrect
The variable 'obj' must have both properties 'x' and 'y'. So accessing obj.x and obj.y prints 10 and 'hello'.
🧠 Conceptual
intermediate1:30remaining
Which statement best describes intersection types in TypeScript?
Choose the correct description of intersection types (&) in TypeScript.
Attempts:
2 left
💡 Hint
Think about combining properties, not choosing one or the other.
✗ Incorrect
Intersection types combine all properties from both types, so the resulting type has everything from both.
🔧 Debug
advanced2:00remaining
What error does this TypeScript code raise?
Analyze this code snippet and select the error it produces.
Typescript
type C = { a: number } & { a: string };
const val: C = { a: 5 };Attempts:
2 left
💡 Hint
Check the property 'a' types in both intersected types.
✗ Incorrect
The property 'a' is declared as number in one type and string in the other. Intersection requires both, which is impossible, so TypeScript errors.
📝 Syntax
advanced1:30remaining
Which option correctly declares an intersection type for two interfaces?
Select the correct syntax to declare a variable with intersection type of interfaces X and Y.
Typescript
interface X { id: number; }
interface Y { name: string; }Attempts:
2 left
💡 Hint
Intersection uses '&' symbol between types.
✗ Incorrect
The '&' symbol combines types to require all properties. Option D uses correct syntax.
🚀 Application
expert2:30remaining
How many properties does the variable 'data' have after this code runs?
Given these types and variable, how many properties does 'data' have?
Typescript
type T1 = { a: number; b: string };
type T2 = { b: string; c: boolean };
const data: T1 & T2 = { a: 5, b: "hello", c: true };Attempts:
2 left
💡 Hint
Intersection merges all properties, duplicates count once.
✗ Incorrect
The intersection type combines all properties from T1 and T2. Property 'b' appears in both but counts once. So properties are 'a', 'b', and 'c' = 3 total.