Challenge - 5 Problems
Intersection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this intersection type example?
Consider the following TypeScript code using intersection types. What will be the output when running this code?
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
Intersection types combine all properties from both types.
✗ Incorrect
The intersection type A & B requires the object to have both properties x and y. The object has both, so logging obj.x and obj.y prints '10 hello'.
❓ Predict Output
intermediate2:00remaining
What happens if properties overlap in intersection types?
Look at this TypeScript code where two types have a property with the same name but different types. What error or output occurs?
Typescript
type C = { a: number };
type D = { a: string };
const val: C & D = { a: 5 };Attempts:
2 left
💡 Hint
Intersection requires the property to satisfy both types simultaneously.
✗ Incorrect
The property 'a' must be both number and string at the same time, which is impossible. TypeScript reports a type error.
🔧 Debug
advanced2:00remaining
Why does this intersection type cause a compile error?
This code tries to assign an object to an intersection type but fails. Identify the cause of the error.
Typescript
type E = { id: number };
type F = { id: string };
const item: E & F = { id: "abc" };Attempts:
2 left
💡 Hint
Check the types of the overlapping property 'id'.
✗ Incorrect
The intersection requires 'id' to be both number and string, which is impossible, causing a compile error.
🧠 Conceptual
advanced2:00remaining
How does intersection type affect method signatures?
Given these two types with methods, what is the type of the method 'foo' in the intersection type G & H?
Typescript
type G = { foo: (x: number) => string };
type H = { foo: (x: string) => number };
// What is the type of foo in G & H?Attempts:
2 left
💡 Hint
Intersection requires the method to satisfy both signatures exactly.
✗ Incorrect
The method signatures conflict because they expect different parameter types and return types. The intersection results in 'never' for foo.
❓ Predict Output
expert2:00remaining
What is the output of this complex intersection type example?
Analyze this TypeScript code using intersection types with optional and required properties. What is the output?
Typescript
type J = { a: number; b?: string };
type K = { b: string; c: boolean };
const obj: J & K = { a: 1, b: "text", c: true };
console.log(obj.a, obj.b, obj.c);Attempts:
2 left
💡 Hint
Intersection combines all properties; optional in one and required in another means required.
✗ Incorrect
Property 'b' is optional in J but required in K, so it must be present. The object has all required properties, so output is '1 text true'.