On this page
Utility Types
TypeScript ships with utility types that derive new types from existing ones. They reduce duplication and make refactors safer.
Partial<T>
Makes every property optional — useful for update payloads:
interface User {
id: number;
name: string;
email: string;
active: boolean;
}
function updateUser(id: number, changes: Partial<User>): User {
const existing: User = { id, name: 'Alice', email: '[email protected]', active: true };
return { ...existing, ...changes };
}
updateUser(1, { email: '[email protected]' }); // OK — only email required
// updateUser(1, { id: 'bad' }); // Error
Required<T>
Opposite of Partial — makes all properties required:
interface Config {
host?: string;
port?: number;
debug?: boolean;
}
type FullConfig = Required<Config>;
// { host: string; port: number; debug: boolean }
Pick<T, Keys>
Select a subset of properties:
interface Product {
id: string;
name: string;
price: number;
description: string;
stock: number;
}
type ProductSummary = Pick<Product, 'id' | 'name' | 'price'>;
const summary: ProductSummary = {
id: 'SKU-1',
name: 'Keyboard',
price: 79.99
};
Omit<T, Keys>
Remove properties from a type:
type ProductInput = Omit<Product, 'id'>;
function createProduct(input: ProductInput): Product {
return { id: crypto.randomUUID(), ...input };
}
createProduct({
name: 'Mouse',
price: 29.99,
description: 'Wireless',
stock: 100
});
Record<Keys, Type>
Build an object type with known keys:
type Role = 'admin' | 'editor' | 'viewer';
const permissions: Record<Role, string[]> = {
admin: ['read', 'write', 'delete'],
editor: ['read', 'write'],
viewer: ['read']
};
type StringRecord = Record<string, number>;
const scores: StringRecord = { alice: 95, bob: 87 };
Readonly<T>
Prevent property reassignment:
const settings: Readonly<Config> = {
host: 'localhost',
port: 3000,
debug: true
};
// settings.port = 8080; // Error
ReturnType and Parameters
Inspect function types:
function fetchUser(id: number): Promise<User> {
return Promise.resolve({ id, name: 'Alice', email: '[email protected]', active: true });
}
type FetchResult = ReturnType<typeof fetchUser>; // Promise<User>
type FetchArgs = Parameters<typeof fetchUser>; // [number]
Combining Utilities
Utility types compose naturally:
type UpdateProductDTO = Partial<Pick<Product, 'name' | 'price' | 'stock'>>;
function patchProduct(id: string, updates: UpdateProductDTO): void {
console.log(`Updating ${id}`, updates);
}
patchProduct('SKU-1', { price: 69.99 });
Built-in utilities cover most everyday transformations. For module organization, continue to the next chapter.