Practical Patterns with Union and Intersection
Apply unions and intersections in real-world scenarios.
Practical Patterns with Union and Intersection is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
Optional Payload Pattern
type Event =
| { type: 'click'; x: number; y: number }
| { type: 'keypress'; key: string }
| { type: 'blur' };Builder/Mixin with Intersection
type Serializable = { serialize(): string };
type Validatable = { validate(): boolean };
type Model = Serializable & Validatable & { id: number };Union for Input Normalization
function toArray<T>(val: T | T[]): T[] {
return Array.isArray(val) ? val : [val];
}Branded Types with Intersection
type UserId = number & { __brand: 'UserId' };
type PostId = number & { __brand: 'PostId' };
// Prevents accidentally mixing IDsWithOptional Pattern
type RequiredFields = { id: number; email: string };
type OptionalFields = Partial<{ name: string; bio: string }>;
type UserUpdate = RequiredFields & OptionalFields;Polymorphic Component Props
type ButtonProps =
| { variant: 'primary'; onClick: () => void }
| { variant: 'link'; href: string };Extending Third-Party Types
import type { Request } from 'express';
type AuthRequest = Request & { user: User };Combining Union and Intersection
type Admin = { role: 'admin' };
type User = { role: 'user' };
type LoggedIn = { sessionId: string };
type AuthUser = (Admin | User) & LoggedIn;Type Guards for Union Members
function isAdmin(u: AuthUser): u is Admin & LoggedIn {
return u.role === 'admin';
}Distributive Behavior in Intersections
type Base = { id: number };
type AB = Base & (string | number);
// equivalent to: (Base & string) | (Base & number)Quick Check
Recap
Frequently asked questions
Is the “Practical Patterns with Union and Intersection” lesson free?
Yes — the full text of “Practical Patterns with Union and Intersection” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Practical Patterns with Union and Intersection”?
Apply unions and intersections in real-world scenarios. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Practical Patterns with Union and Intersection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Union Types: A or B
- Intersection Types: A and B
- Discriminated Unions for Safe Pattern Matching
- Practical Patterns with Union and Intersection