0
0
Typescriptprogramming~5 mins

How intersection combines types in Typescript - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What does an intersection type do in TypeScript?
An intersection type combines multiple types into one. The new type has all properties and methods from each combined type.
Click to reveal answer
beginner
How do you write an intersection type in TypeScript?
Use the & symbol between types. For example: type A = B & C; means A has all properties of B and C.
Click to reveal answer
intermediate
What happens if two intersected types have properties with the same name but different types?
TypeScript requires the property to satisfy both types. If they conflict, the intersection becomes impossible to satisfy and causes an error or results in the never type.
Click to reveal answer
beginner
Example: What properties does this type have?<br>type A = {name: string};<br>type B = {age: number};<br>type C = A & B;
Type C has both name (string) and age (number) properties. It combines all properties from A and B.
Click to reveal answer
intermediate
Can intersection types combine primitive types like string & number?
No. Intersection of incompatible primitive types like string & number results in never, meaning no value can satisfy both.
Click to reveal answer
What symbol is used to create an intersection type in TypeScript?
A&
B|
C+
D*
If type A = {x: number} and type B = {y: string}, what properties does A & B have?
AOnly x
BOnly y
CBoth x and y
DNeither x nor y
What happens if two intersected types have the same property name but different types?
ATypeScript merges them as a union
BTypeScript picks the first type
CTypeScript ignores one property
DTypeScript throws an error or results in never
What is the result of string & number in TypeScript?
Anever
Bnumber
Cstring
Dstring | number
Which of these is a valid intersection type?
Atype C = {a: number} + {b: string};
Btype C = {a: number} & {b: string};
Ctype C = {a: number} | {b: string};
Dtype C = {a: number} - {b: string};
Explain in your own words how intersection types combine multiple types in TypeScript.
Think about how two sets of properties join together.
You got /4 concepts.
    Describe what happens when two intersected types have the same property name but different types.
    Consider if a property must be both a string and a number at the same time.
    You got /3 concepts.