Sub topic 9
Working with Members: `Extract` and Indexed Access
Assume you know object types, union types, generics, and type narrowing. This chapter focuses on inspecting and selecting members of a discriminated union.
A discriminated union has several object types that share a property with literal values. For example:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number };Each member has a different kind value. TypeScript uses that value to identify the member.
Selecting one member with `Extract`
Extract<Union, Condition> is a utility type that keeps the members of Union assignable to Condition.
Use it when you want a named type for one variant:
type Circle = Extract<Shape, { kind: "circle" }>;
// { kind: "circle"; radius: number }
type Rectangle = Extract<Shape, { kind: "rectangle" }>;
// { kind: "rectangle"; width: number; height: number }The condition does not need to list every property. The kind property is enough to select the correct member.
You can use the extracted type in functions:
type Circle = Extract<Shape, { kind: "circle" }>;
function radiusOf(circle: Circle): number {
return circle.radius;
}
console.log(radiusOf({ kind: "circle", radius: 5 }));Passing a square to radiusOf causes a type error because a square does not have a radius.
Getting all tag values with indexed access
An indexed access type reads a property type from another type. Its syntax resembles property access, but it operates at the type level.
type ShapeKind = Shape["kind"];
// "circle" | "square" | "rectangle"Because Shape is a union, Shape["kind"] means “the type of kind from every member.” TypeScript combines those values into a union of literals.
You can use this type to constrain a function:
type ShapeKind = Shape["kind"];
function isShapeKind(value: string): value is ShapeKind {
return value === "circle" ||
value === "square" ||
value === "rectangle";
}
const input = "circle";
if (isShapeKind(input)) {
const kind: ShapeKind = input;
console.log(kind);
}This creates a single source of truth for the allowed tag values. If you add another member to Shape, ShapeKind includes its tag automatically.
Combining `Extract` and indexed access
These tools work well together when a function accepts a tag and needs the matching member type:
type ShapeKind = Shape["kind"];
type ShapeOfKind<K extends ShapeKind> =
Extract<Shape, { kind: K }>;
type Circle = ShapeOfKind<"circle">;
// { kind: "circle"; radius: number }
type Square = ShapeOfKind<"square">;
// { kind: "square"; size: number }The generic parameter K must be one of the known discriminant values. ShapeOfKind<"circle"> selects the circle member.
Choosing the right tool
| Goal | Type expression | Result | Column | Column |
|---|---|---|---|---|
| Select the circle member | `Extract<Shape, { kind: "circle" }>` | The circle object type | ||
| Get every tag value | `Shape["kind"]` | `"circle" \ | "square" \ | "rectangle"` |
| Select by a generic tag | `Extract<Shape, { kind: K }>` | The member matching `K` |
Exercise
Define a type named Square by selecting the square member from Shape with Extract.
Show answer
type Square = Extract<Shape, { kind: "square" }>;