Sub topic 13
Limits and Pitfalls of TypeScript Utility Types
Assume you know basic TypeScript annotations, interfaces, type aliases, unions, string literal types, and generic syntax. Utility types transform types during compilation. They do not validate or change JavaScript values at runtime. Most mistakes happen when a type transformation is shallower or less distributive than expected.
Utility types do not validate runtime data
Partial<User> describes which properties TypeScript allows in a value. It does not check data received from JSON, a form, or an API while the program runs.
interface User {
id: number;
name: string;
}
type UserPatch = Partial<User>;
const fromJson: unknown = JSON.parse('{"name": "Ada"}');
// This assertion changes only the compiler's view.
// It does not validate the object.
const patch = fromJson as UserPatch;
console.log(patch);If the JSON contains { "id": "not a number" }, the assertion does not detect the problem. Use a runtime validation library or explicit checks when data crosses a trust boundary.
`Partial` is shallow
Partial<T> makes only the direct properties of T optional. It does not recursively make nested properties optional.
interface Settings {
theme: {
color: string;
fontSize: number;
};
}
type SettingsPatch = Partial<Settings>;
const patch: SettingsPatch = {
// The theme property is optional, but if present,
// its color and fontSize are still required.
theme: {
color: "dark",
// fontSize is missing, so this is an error.
},
};
type DeepPartial<T> = T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T;
const nestedPatch: DeepPartial<Settings> = {
theme: { color: "dark" },
};A recursive utility needs decisions for arrays, functions, dates, maps, and other special objects. The simple DeepPartial above is useful for plain data shapes, but it is not a universal solution.
`Omit` can lose union-specific properties
A discriminated union contains several object shapes distinguished by a shared literal property.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number };
type WithoutKind = Omit<Shape, "kind">;
// This can become {} because keyof Shape contains only shared keys.
type DistributiveOmit<T, K extends keyof any> =
T extends unknown ? Omit<T, K> : never;
type ShapeData = DistributiveOmit<Shape, "kind">;
// { radius: number } | { side: number }The conditional type distributes over each union member because T appears directly on the left side of extends. This preserves the separate shapes.
Other common surprises
| Utility type | Common limit | Safer question |
|---|---|---|
| `Readonly<T>` | Prevents assignments through that type, but is shallow | Do nested values also need protection? |
| `Record<K, V>` | A broad key such as `string` permits any string key | Should the keys be a literal union? |
| `Pick<T, K>` | Keeps only named properties, not runtime fields | Is the resulting object actually checked? |
| `Required<T>` | Removes optional markers, but cannot create missing data | Has the value been validated first? |
A Readonly property can still refer to a mutable array:
interface Report {
tags: string[];
}
const report: Readonly<Report> = { tags: ["draft"] };
// report.tags = []; // Error
report.tags.push("review"); // Allowed: the array itself is still mutable.For a lookup table, prefer a literal union when every key must be present:
type Status = "idle" | "loading" | "done";
const labels: Record<Status, string> = {
idle: "Waiting",
loading: "Loading",
done: "Complete",
// Missing one status causes a compile error.
};Before composing utility types, identify whether you need shallow or recursive behavior, whether a union must distribute, and whether runtime validation is required. These questions prevent a convenient type from giving a misleading sense of safety.
Exercise
Why does Partial<Settings> still require color and fontSize when theme is present, and what type can allow only color to be supplied?
Show answer
Partial<Settings> is shallow, so it changes only theme's optionality. A recursive type such as DeepPartial<Settings> makes nested properties optional.