Loading Blog Article...
Loading Blog Article...
Loading Blog Article...
TypeScript utility types are often introduced as “nice-to-have” helpers. In reality, they are core tools that shape how scalable, safe, and maintainable your codebase becomes.
In this article, based on a great breakdown video on TypeScript utility types, I’ll walk through the most important ones with concrete, real-world examples — not abstract theory.
As applications grow, you constantly need to:
Utility types allow you to do all of this without rewriting types by hand.
You have a full model, but an API or UI only needs part of it.
type User = {
id: string
email: string
password: string
createdAt: Date
}
type PublicUser = Pick<User, "id" | "email">
✅ Common in:
You want everything except certain fields.
type SafeUser = Omit<User, "password">
This is extremely common when:
PATCH APIs where fields are optional.
type UpdateUserRequest = Partial<User>
Instead of writing:
type UpdateUserRequest = {
id?: string
email?: string
password?: string
createdAt?: Date
}
}You want to ensure all fields are present at a certain stage.
type DraftUser = Partial<User>
type FinalUser = Required<DraftUser>
Great for:
Prevent accidental mutation.
type ReadonlyUser = Readonly<User>
// ❌ Error
readonlyUser.email = "new@email.com"
Widely used in:
Mapping keys to values with strict typing.
type Role = "admin" | "user" | "guest"
const permissions: Record<Role, string[]> = {
admin: ["read", "write", "delete"],
user: ["read", "write"],
guest: ["read"]
}
Much safer than { [key: string]: any }.
Manipulating union types.
type Status = "idle" | "loading" | "success" | "error"
type ActiveStatus = Exclude<Status, "idle">
type ErrorOrSuccess = Extract<Status, "error" | "success">
Perfect for:
Avoid duplicating return types.
function getUser() {
return { id: "1", email: "test@test.com" }
}
type UserResponse = ReturnType<typeof getUser>
If the function changes, the type updates automatically.
function login(email: string, password: string) {}
type LoginArgs = Parameters<typeof login>
// [string, string]
Useful for:
class UserService {
constructor(private apiUrl: string) {}
}
type ServiceArgs = ConstructorParameters<typeof UserService>
// [string]
type ServiceInstance = InstanceType<typeof UserService>
Very helpful in:
type MaybeUser = User | null | undefined
type SafeUser = NonNullable<MaybeUser>
Great for:
type Result = Awaited<Promise<User>>
Essential when working with:
type EventName = "user_created"
type Upper = Uppercase<EventName> // "USER_CREATED"
type Capital = Capitalize<EventName> // "User_created"
Useful for:
Knowing these utility types isn’t about memorization. It’s about recognizing patterns and letting TypeScript do the heavy lifting.
If you use these daily:
That’s one of the clearest signs of moving from junior to senior TypeScript development.
If you want a follow-up article with advanced combinations (infer, conditional types, real API schemas), let me know.
Back