Utility Types
Built-in TypeScript utility types
#utility-types #partial #pick #omit #record
Utility Types
TypeScript provides built-in utility types for common type transformations.
Partial & Required
interface User {
name: string;
age: number;
email: string;
}
type PartialUser = Partial<User>; // All optional
type RequiredUser = Required<User>; // All required
const update: PartialUser = { age: 31 };
Pick & Omit
interface User {
id: number;
name: string;
email: string;
password: string;
}
type PublicUser = Omit<User, "password">;
type UserCredentials = Pick<User, "email" | "password">;
Record
type Role = "admin" | "user" | "guest";
const permissions: Record<Role, string[]> = {
admin: ["read", "write", "delete"],
user: ["read", "write"],
guest: ["read"]
};
ReturnType & Parameters
function getUser() {
return { id: 1, name: "Alice" };
}
type User = ReturnType<typeof getUser>;
function greet(name: string, age: number) {
console.log(`${name} is ${age}`);
}
type GreetParams = Parameters<typeof greet>; // [string, number]
Discover another handy tool from EditPDF.pro