Built-in Utility Types
📖 Concept
TypeScript ships with built-in utility types that transform existing types — making properties optional, required, readonly, or extracting/excluding parts.
Object transformers:
Partial<T>— All properties optionalRequired<T>— All properties requiredReadonly<T>— All properties readonlyPick<T, K>— Select specific propertiesOmit<T, K>— Remove specific propertiesRecord<K, V>— Object with keys K and values V
Union transformers:
Exclude<T, U>— Remove types from unionExtract<T, U>— Keep only matching typesNonNullable<T>— Remove null and undefined
Function transformers:
ReturnType<T>— Extract return typeParameters<T>— Extract parameter types as tupleConstructorParameters<T>— Parameters of a constructor
String transformers:
Uppercase<T>,Lowercase<T>,Capitalize<T>,Uncapitalize<T>
Understanding how these work internally (using mapped types, conditional types, and infer) is key to building your own utility types in Phase 3.
🏠 Real-world analogy: Utility types are like photo filters. You take an existing photo (type) and apply transformations — make it grayscale (Readonly), crop it (Pick), blur parts (Omit). The original photo remains unchanged; you get a new version.
💻 Code Example
1interface User {2 id: number;3 name: string;4 email: string;5 age: number;6 role: "admin" | "user";7}89// Partial — all properties become optional10type UpdateUser = Partial<User>;11// { id?: number; name?: string; email?: string; ... }12function updateUser(id: number, updates: Partial<User>): void {13 // Can pass any subset of User properties14}15updateUser(1, { name: "Bob" }); // ✅16updateUser(1, { name: "Bob", age: 31 }); // ✅1718// Required — opposite of Partial19interface Config {20 apiUrl?: string;21 timeout?: number;22}23type StrictConfig = Required<Config>;24// { apiUrl: string; timeout: number; } — no more optional!2526// Readonly — prevent mutation27type ImmutableUser = Readonly<User>;28// const u: ImmutableUser = { ... };29// u.name = "Bob"; // ❌ Error: readonly3031// Pick — select properties32type UserPreview = Pick<User, "id" | "name">;33// { id: number; name: string; }3435// Omit — remove properties36type UserWithoutEmail = Omit<User, "email">;37// { id: number; name: string; age: number; role: ... }3839// Record — typed key-value map40type UserRoles = Record<string, "admin" | "user" | "guest">;41const roles: UserRoles = {42 alice: "admin",43 bob: "user"44};4546type StatusMessages = Record<"loading" | "success" | "error", string>;47const messages: StatusMessages = {48 loading: "Please wait...",49 success: "Done!",50 error: "Something went wrong"51};5253// Exclude and Extract (on unions)54type AllTypes = string | number | boolean | null | undefined;55type NotNull = Exclude<AllTypes, null | undefined>;56// string | number | boolean57type OnlyStringOrNumber = Extract<AllTypes, string | number>;58// string | number5960// NonNullable61type MaybeString = string | null | undefined;62type DefiniteString = NonNullable<MaybeString>; // string6364// ReturnType — extract function return type65function fetchUsers() {66 return [{ id: 1, name: "Alice" }];67}68type Users = ReturnType<typeof fetchUsers>;69// { id: number; name: string; }[]7071// Parameters — extract function params as tuple72function createUser(name: string, age: number, role?: string) { }73type CreateUserParams = Parameters<typeof createUser>;74// [name: string, age: number, role?: string]7576// Combining utility types77type CreateUserDTO = Omit<User, "id">; // Everything except id78type UserPatch = Partial<Omit<User, "id" | "role">>; // Optional fields, no id/role7980// Awaited — extract the resolved type of a Promise81type PromiseResult = Awaited<Promise<Promise<string>>>;82// string (deeply unwraps)
🏋️ Practice Exercise
Mini Exercise:
- Create an
UpdateDTOtype that makes all fields ofUseroptional EXCEPTid(required) - Use
Recordto create a typed dictionary mapping HTTP status codes to messages - Extract the return type of an async function using
Awaited<ReturnType<typeof fn>> - Create a
PublicUsertype that omits sensitive fields likepasswordandemail - Combine
Pick,Partial, andRequiredto create a complex DTO type
⚠️ Common Mistakes
Confusing
OmitandExclude—Omitworks on object types (removes properties);Excludeworks on union types (removes union members)Using
Partialwhen only some fields should be optional — usePick+Partialcombination or intersection for precise controlForgetting that
Readonlyis shallow — nested objects inside aReadonly<T>can still be mutatedNot knowing that
Record<string, T>allows ANY string key — for specific keys, useRecord<'a' | 'b', T>Using
ReturnTypewith a function value instead of a type — useReturnType<typeof myFunction>, notReturnType<myFunction>
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Built-in Utility Types