GenericsLesson 4.4
Built-in TypeScript utility types Partial, Required, Readonly, Pick, Omit
Partial type, Required type, Readonly type, Pick type, Omit type, practical use cases for each, combining utility types
Utility types overview
TypeScript ships built-in utility types that transform existing types without rewriting them:
interface User {
id: number;
name: string;
email: string;
password: string;
}
type PartialUser = Partial; // all fields optional
type RequiredUser = Required>; // all fields required
type ReadonlyUser = Readonly; // all fields readonly
type PublicUser = Omit; // remove password
type Credentials = Pick; // keep only theseReal-world patterns
// Update endpoint: only send changed fields
function updateUser(id: number, changes: Partial>) {
// changes has optional name, email, password — no id
}
// Create endpoint: no id (auto-generated)
type CreateUserDTO = Omit;
function createUser(dto: CreateUserDTO): User {
return { id: Date.now(), ...dto };
}Composing utility types is idiomatic TypeScript. You rarely need to manually rewrite types when these tools cover the common transformations.
