Intersections
Combine with &
An intersection glues two object types together with the & sign. The result has every field from both sides at once.
Person has name from Named and age from Aged, so the const ada must provide both fields.
type Named = { name: string; }; type Aged = { age: number; }; type Person = Named & Aged; const ada: Person = { name: "Ada", age: 36 }; console.log(ada.name, ada.age);
Ada 36
Mixin behavior
A mixin is a small reusable piece of data added to many types. Timestamped can be mixed into articles, comments, and anything else with a creation date.
Article keeps its own title and gains createdAt from the mixin. Each side stays small and easy to reuse.
type Timestamped = { createdAt: string; }; type Article = { title: string; } & Timestamped; const post: Article = { title: "Hello", createdAt: "2026-01-01", }; console.log(post.title, post.createdAt);
Hello 2026-01-01
Intersection vs union
Unions and intersections answer opposite questions. A union value may be one side or the other, while an intersection value must satisfy every side at once.
Either accepts an a object or a b object, but Both demands a and b together. Pick the one that matches the real rule.
type Either = { a: string } | { b: number }; type Both = { a: string } & { b: number }; const both: Both = { a: "x", b: 2 }; console.log(both.a, both.b);
x 2
Flatten carefully
Intersections of clashing fields collapse to never, which no value can satisfy. A Conflict with id as both string and number can never be built.
Keep intersections flat and group related fields into one base type instead. Small named pieces compose without surprises.
type Conflict = { id: string } & { id: number }; // id is never, so no value fits const bad: Conflict = { id: "a1" }; console.log(bad);
main.ts(4,25): error TS2322: Type 'string' is not assignable to type 'never'.
A value that satisfies both sides would need an id that is a string and a number at once, so the assignment cannot compile.
type Clean = { id: string } & { label: string }; const ok: Clean = { id: "a1", label: "first" }; console.log(ok.id, ok.label);
a1 first
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What must a value of the type
Named & Agedprovide?Answer
Both a name and an age
An intersection demands every field of every side at once.
-
How does an intersection differ from a union?
Answer
An intersection value satisfies every side; a union value satisfies one
The union is one side or the other, and the intersection is all sides together.
-
What happens when two intersected fields have incompatible types?
Answer
The field becomes
never, so no value fitsA value would need both types at once, and the only type that satisfies both is never.
-
What is a mixin type used for?
Answer
Adding one small reusable piece of data to many types
A mixin such as Timestamped is intersected into each type that needs the same field.
-
What does this program print?
type Named = { name: string }; type Aged = { age: number }; type Person = Named & Aged; const ada: Person = { name: "Ada", age: 36 }; console.log(ada.name, ada.age);Answer
Ada 36
The object provides both fields of the intersection, and the log prints them in order.
-
What does this program print?
type Timestamped = { createdAt: string }; type Article = { title: string } & Timestamped; const post: Article = { title: "Hello", createdAt: "2026-01-01", }; console.log(post.title, post.createdAt);Answer
Hello 2026-01-01
The intersection adds the mixin field, and the log prints the title first.
-
What does this program print?
type Either = { a: string } | { b: number }; type Both = { a: string } & { b: number }; const both: Both = { a: "x", b: 2 }; console.log(both.a, both.b);Answer
x 2
Both needs the two fields, and the object supplies them.
-
What does this program print?
type Clean = { id: string } & { label: string }; const ok: Clean = { id: "a1", label: "first" }; console.log(ok.id, ok.label);Answer
a1 first
The two sides have different fields, so the object satisfies both and prints both.
-
What does the compiler say about this program?
type Conflict = { id: string } & { id: number }; const bad: Conflict = { id: "a1" }; console.log(bad);Answer
error TS2322: Type 'string' is not assignable to type 'never'.
The clashing id field collapses to never, and a string cannot be stored there.
-
What does the compiler say about this program?
type Named = { name: string }; type Aged = { age: number }; type Person = Named & Aged; const p: Person = { name: "Ada" }; console.log(p);Answer
error TS2322: Type '{ name: string; }' is not assignable to type 'Person'.
The object lacks a field of the intersection, and the compiler names the type it cannot fill.
Article author: Arthur Isaev