Built-in utility types
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Apply Partial, Required, Readonly, Pick, Omit and Record.
- Use ReturnType and Parameters to derive function shapes.
- Choose the smallest utility that expresses the intent.
Overview
#Object transforms
type Draft = Partial<User>; // every property optional
type Locked = Readonly<User>; // every property readonly
type NameOnly = Pick<User, "name">;
type WithoutId = Omit<User, "id">;
type ById = Record<string, User>;
Required<T> is the inverse of Partial<T>. Pick and Omit select or drop keys.
Function-derived types
function createUser(name: string, age: number): User { /* ... */ }
type Args = Parameters<typeof createUser>; // [string, number]
type Result = ReturnType<typeof createUser>; // User
typeof createUser reads the value's type; the utilities then extract from it.
Async and unions
Awaited<T> unwraps a promise type. NonNullable<T> removes null and undefined. Exclude and Extract filter a union.
type Clean = NonNullable<string | null>; // string
type OnlyStrings = Extract<string | number, string>; // string
Choosing
Reach for the built-in that names your intent. If none fits, the next lessons show how to build your own.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Given `interface User { id: string; name: string; age: number }`, implement `updateUser(user: User, changes: Partial<User>): User` that returns a merged user.
starter.typescriptexport interface User { id: string; name: string; age: number } export function updateUser(user: User, changes: Partial<User>): User { // your code }Reveal the reference answer
reference.typescriptexport interface User { id: string; name: string; age: number } export function updateUser(user: User, changes: Partial<User>): User { return { ...user, ...changes }; }Authored expected output: Compiles with no errors. Runtime: updateUser({ id: "1", name: "A", age: 1 }, { age: 2 }).age is 2.
Partial lets callers pass any subset of fields while the result stays a full User.
Exercise 2Predict
What is the type of `R`? ```ts function make(): number[] { return []; } type R = ReturnType<typeof make>; ```
Exercise 3Implement
Implement `toSummary(user: User): Omit<User, "age">` that returns the user without the age field.
starter.typescriptexport interface User { id: string; name: string; age: number } export function toSummary(user: User): Omit<User, "age"> { // your code }Reveal the reference answer
reference.typescriptexport interface User { id: string; name: string; age: number } export function toSummary(user: User): Omit<User, "age"> { const { age, ...rest } = user; return rest; }Authored expected output: Compiles with no errors. Runtime: toSummary({ id: "1", name: "A", age: 3 }) is { id: "1", name: "A" }.
Omit shapes the return type; destructuring removes the field at runtime.
Exercise 4Interview
A teammate writes `type Patch = Partial<Omit<User, "id">>` for an update endpoint. Explain what this type expresses and why it is a good choice.
Reveal the reference answer
reference.typescriptOmit first removes id, which should never be patched, then Partial makes every remaining field optional so a client can send only what changes. The result is a type that allows any subset of the mutable fields and forbids changing the identifier, which matches the endpoint's real contract.Authored expected output: A written answer naming both utilities and the intent.
Composing utilities expresses a precise contract with no runtime cost.
Common misconceptions
#- Partial makes values optional at runtime.
- ReturnType works on a value directly without typeof.
- Omit removes properties from the runtime object.
Follow-up practice
#- typescript-keyof-indexed-satisfies
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.