0
0
Typescriptprogramming~5 mins

Intersection type syntax in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an intersection type in TypeScript?
An intersection type combines multiple types into one. It means a value must satisfy all the combined types at the same time.
Click to reveal answer
beginner
How do you write an intersection type in TypeScript?
Use the ampersand (&) symbol between types. For example: type A = Type1 & Type2;
Click to reveal answer
intermediate
What happens if two intersected types have conflicting properties?
TypeScript will require the property to satisfy both types. If conflicts can't be resolved, it causes a type error.
Click to reveal answer
beginner
Example: What does this type mean? <br>type Person = { name: string } & { age: number };
It means a Person must have both a name (string) and an age (number).
Click to reveal answer
intermediate
Can intersection types combine interfaces and types?
Yes, you can intersect interfaces and type aliases together using & to create a new combined type.
Click to reveal answer
Which symbol is used for intersection types in TypeScript?
A|
B&
C+
D*
What does this intersection type require? <br>{a: number} & {b: string}
AAn object with both a and b
BAn object with a or b but not both
CAn object with either a or b
DAn object with neither a nor b
Can intersection types be used to combine primitive types like string & number?
AYes, it creates a union
BNo, it creates an array
CYes, it creates a tuple
DNo, it results in never type
If two intersected types have the same property with different types, what happens?
ATypeScript picks the first type
BTypeScript picks the second type
CTypeScript throws a type error
DTypeScript ignores the property
Which of these is a valid intersection type?
Atype T = {x: number} & {y: string};
Btype T = {x: number} | {y: string};
Ctype T = {x: number} + {y: string};
Dtype T = {x: number} - {y: string};
Explain in your own words what an intersection type is and how it is used in TypeScript.
Think about how two sets overlap and what belongs to both.
You got /3 concepts.
    Describe a situation where using an intersection type would be helpful in a program.
    Imagine you want an object that has features from two different things.
    You got /3 concepts.