0
0
Typescriptprogramming~20 mins

How intersection combines types in Typescript - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Intersection Mastery
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 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);
A10 undefined
Bundefined hello
CType error at assignment
D10 hello
Attempts:
2 left
💡 Hint
Intersection types combine all properties from both types.
Predict Output
intermediate
2: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 };
AType error: Type 'number' is not assignable to type 'string & number'
BNo error, val.a is 5
CRuntime error: conflicting types
Dval.a is '5' as string
Attempts:
2 left
💡 Hint
Intersection requires the property to satisfy both types simultaneously.
🔧 Debug
advanced
2: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" };
AError because object is missing property from E
BError because 'id' cannot be both number and string
CNo error, assignment is valid
DError because 'id' is missing in object
Attempts:
2 left
💡 Hint
Check the types of the overlapping property 'id'.
🧠 Conceptual
advanced
2: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?
Afoo: never (no compatible type)
Bfoo: (x: number | string) => string | number
Cfoo: (x: number) => string
Dfoo: (x: string) => number
Attempts:
2 left
💡 Hint
Intersection requires the method to satisfy both signatures exactly.
Predict Output
expert
2: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);
AType error: Property 'b' is optional in J but required in K
B1 undefined true
C1 text true
DType error: Missing property 'c'
Attempts:
2 left
💡 Hint
Intersection combines all properties; optional in one and required in another means required.