Lesson

TypeScript Advanced Types

10 min reading
Free Course

TypeScript Advanced Types: Intersections, Recursive & Template Types

Beyond basic interfaces and unions, TypeScript's type system supports intersection types (&), template literal types, and recursive types. These features let you compose small reusable building blocks into exact API and UI contracts without copy-pasting properties.

Think of & as merging two object contracts together—the resulting value must satisfy every property from both types.

Intersections, Template Literals, and Recursive Trees

Template literal types build string unions dynamically using ${...} syntax, while recursive types model nested trees like folder structures or comment threads.

type Timestamped = { createdAt: string; updatedAt: string };
type AuditUser = { updatedBy: string };
type AuditedRecord = Timestamped & AuditUser & { id: number };

type HttpMethod = "GET" | "POST";
type RouteKey = `${HttpMethod} /api/${string}`;

interface MenuNode {
  label: string;
  href?: string;
  children?: MenuNode[];
}

const route: RouteKey = "GET /api/orders";
const sidebar: MenuNode = {
  label: "Docs",
  children: [{ label: "Intro", href: "/docs/intro" }],
};
console.log(route, sidebar.children?.[0]?.label);

Built-In String Transformation Helpers

TypeScript includes Uppercase<S>, Lowercase<S>, Capitalize<S>, and Uncapitalize<S> to reshape template literal string types cleanly (for example, turning "click" into "onClick"). Pair these helpers with union types whenever you generate typed event names, CSS property keys, or Redux-style action strings across shared modules.

Common Mistakes to Avoid

  • Intersecting incompatible primitive types: string & number collapses to never because no value can be both a string and a number simultaneously.
  • Intersecting two interfaces with conflicting field types: Merging { id: string } & { id: number } makes id type never, causing confusing errors later.
  • Building huge Cartesian template unions: Combining 4 unions of 10 strings each generates 10,000 combinations and slows down your IDE.

Frequently Asked Questions

When should I use interface extends vs & intersection?

Prefer interface B extends A when extending object shapes because tsc detects conflicting property types immediately at declaration time instead of silently collapsing to never.

Can template literal types validate URLs or CSS units?

Yes. type CssSize = ${number}px|${number}rem`` accepts "16px" and "1.5rem" while rejecting "16pt".

How does Awaited<T> help with advanced async types?

Awaited<Promise<Promise<User>>> recursively unwraps nested Promises down to User.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum