Sub topic 10
Built-in Utility Types
This chapter assumes you know basic TypeScript types, interfaces, type aliases, function annotations, unions, and classes. It also assumes you can read a generic type such as Box<string>.
Utility types are generic types provided by TypeScript. They transform an existing type into a new type. They help you avoid repeating object shapes while keeping the relationship between types visible to the compiler.
`Partial` for Incomplete Objects
Partial<T> makes every property in T optional. It is useful for update operations where a caller may provide only the fields that should change.
interface Todo {
title: string;
done: boolean;
notes: string;
}
type TodoUpdate = Partial<Todo>;
function updateTodo(id: number, changes: TodoUpdate): void {
console.log(id, changes);
}
updateTodo(1, { done: true });
updateTodo(1, { title: "Review generics", notes: "Chapter 4" });TodoUpdate still rejects unknown properties:
// updateTodo(1, { priority: "high" }); // ErrorPartial changes whether properties are required. It does not change their value types.
`Pick` and `Omit` for Selecting Properties
Pick<T, K> creates a type containing only the keys in K. The keys must come from keyof T, which is the union of property names in T.
type TodoSummary = Pick<Todo, "title" | "done">;
const summary: TodoSummary = {
title: "Review generics",
done: false
};Use Pick when an API should expose a deliberate subset of an object. This can prevent implementation details such as notes from appearing in a summary response.
Omit<T, K> does the opposite. It creates a type with the listed keys removed.
type TodoWithoutNotes = Omit<Todo, "notes">;
const todo: TodoWithoutNotes = {
title: "Review generics",
done: false
};The type describes what the compiler accepts. It does not remove properties at runtime.
`Record` for Keyed Data
Record<K, V> creates an object type whose keys come from K and whose values have type V.
type Theme = "light" | "dark";
const themeLabels: Record<Theme, string> = {
light: "Light mode",
dark: "Dark mode"
};Because Theme is a union, both keys are required. Adding an unsupported key or omitting a required key causes a type error.
Record is useful for lookup tables, feature flags, and configuration maps.
type Feature = "search" | "export" | "billing";
const enabled: Record<Feature, boolean> = {
search: true,
export: false,
billing: true
};`ReturnType` for Function Results
ReturnType<F> extracts the return type from a function type.
function makeTodo(): Todo {
return {
title: "Write",
done: false,
notes: ""
};
}
type MadeTodo = ReturnType<typeof makeTodo>; // Todo
const result: MadeTodo = makeTodo();typeof makeTodo means “the type of this function.” ReturnType then reads the type produced when that function returns.
Choosing the Right Utility
Common Errors
- Use
Partialfor patches, not as a replacement for the complete domain type. - Use
PickorOmitwhen an API boundary should expose fewer fields. - Prefer a union of literal keys with
Recordwhen every key must be present. - Remember that utility types do not validate untyped JSON. Validate external data before treating it as a typed value.
- Do not use
ReturnTypeto hide a confusing function signature. Name an explicit return type when readers need to understand the contract directly.
Exercise
What type should you use for an object that must contain admin and editor keys, with a boolean value for each key?
Show answer
Record<"admin" | "editor", boolean>.