DAY 7 OF 30
Intersection types
Yesterday, in one sentence
A name for a type, written with the type keyword. It can be passed like a
variable wherever a type is needed, which makes the type reusable instead of
written out every time.
Did you say:
- ✓a name for a type
- ✓written with the `type` keyword
- ✓makes the type reusable
Interview question
Yes, with a ? after the property name. The object does not have to include it,
and reading it gives the declared type or undefined.
Did you say:
- ✓yes
- ✓`?` after the property name
- ✓reading it gives the type or `undefined`
An intersection type combines two or more types into one that has everything from
all of them. You write the types separated by an &.
type Contact = { email: string };
type Named = { name: string };
type Person = Contact & Named; // intersection type
const logan: Person = {
email: "logan@mansion.com",
name: "Logan",
};
const scott: Person = { name: "Scott" };
// ❌ Property 'email' is missing in type '{ name: string; }'
A union typeUnion Type written with | means one type or the
other. An intersection type written with & means both at once.
Intersection types are how you build a bigger shape out of smaller ones instead of repeating properties.
type Timestamps = {
readonly createdAt: string;
updatedAt?: string;
};
type Mutant = Named &
Timestamps & {
power: string;
};
const jean: Mutant = {
name: "Jean",
power: "telepathy",
createdAt: "2026-01-01",
};
Check yourself
type Person = { name: string } & { age: number }, which object is valid?An intersection type requires everything from both types, so the object needs both properties, each with the right type.
A union type means the value is one of several types, written with |. An
intersection type means the value has everything from all of them at once,
written with &.
Did you say:
- ✓union: one of several types, written with `|`
- ✓intersection: everything from all of them, written with `&`