0
0
Typescriptprogramming~20 mins

Intersection type syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Intersection Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Aundefined undefined
BType error: Property 'y' is missing
C10 hello
D10 undefined
Attempts:
2 left
💡 Hint
An intersection type combines all properties from both types.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes intersection types in TypeScript?
Choose the correct description of intersection types (&) in TypeScript.
AThey create a type that has properties from both types combined.
BThey create a type that can be either one type or the other.
CThey remove common properties from both types.
DThey create a union of all possible values from both types.
Attempts:
2 left
💡 Hint
Think about combining properties, not choosing one or the other.
🔧 Debug
advanced
2: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 };
AType error: Type 'number' is not assignable to type 'string'.
BSyntax error: Invalid intersection type.
CNo error, val is valid.
DRuntime error: Cannot assign number to string.
Attempts:
2 left
💡 Hint
Check the property 'a' types in both intersected types.
📝 Syntax
advanced
1: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; }
Alet obj: X && Y = { id: 1, name: "Alice" };
Blet obj: X | Y = { id: 1, name: "Alice" };
Clet obj: X, Y = { id: 1, name: "Alice" };
Dlet obj: X & Y = { id: 1, name: "Alice" };
Attempts:
2 left
💡 Hint
Intersection uses '&' symbol between types.
🚀 Application
expert
2: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 };
A4
B3
C1
D2
Attempts:
2 left
💡 Hint
Intersection merges all properties, duplicates count once.