Mapped Types Toolkit
What you'll build
A set of utility types reimplementing TypeScript's built-in helpers, plus a custom DeepPartial that makes all nested properties optional.
Goals
- Use [K in keyof T] syntax to map over keys
- Add or remove modifiers with ? and -?
- Build Pick and Omit with key filtering
- Implement recursive DeepPartial
Prerequisites
- Generic types
- keyof and indexed access types
- Conditional types (from project-11)
Steps
- 1
Build Partial and Readonly
Partial<T> makes all properties optional; Readonly<T> makes them readonly. Both use mapped types with modifiers.
Codetype MyPartial<T> = { [K in keyof T]?: T[K]; }; type MyReadonly<T> = { readonly [K in keyof T]: T[K]; }; interface User { id: number; name: string; email: string; } type UserPartial = MyPartial<User>; // { id?: number; name?: string; email?: string; } type UserReadonly = MyReadonly<User>; // readonly id, name, emailExpected result
UserPartial allows partial objects; UserReadonly disallows mutation.
- 2
Build Required
Required<T> removes the optional modifier. Use -? to strip the ? from each property.
Codetype MyRequired<T> = { [K in keyof T]-?: T[K]; }; type MaybeUser = { id?: number; name?: string; }; type RequiredUser = MyRequired<MaybeUser>; // { id: number; name: string; }Expected result
RequiredUser has all properties required.
- 3
Build Pick and Omit
Pick<T, K> keeps only keys in K; Omit<T, K> removes them. Pick uses a mapped type over K; Omit is Pick + Exclude.
Codetype MyPick<T, K extends keyof T> = { [P in K]: T[P]; }; type MyOmit<T, K extends keyof T> = MyPick<T, MyExclude<keyof T, K>>; type UserPreview = MyPick<User, 'id' | 'name'>; // { id: number; name: string; } type UserWithoutId = MyOmit<User, 'id'>; // { name: string; email: string; }Expected result
UserPreview has only id and name; UserWithoutId excludes id.
- 4
Build Record
Record<K, V> creates an object type with keys K and values V. Use a mapped type over K.
Codetype MyRecord<K extends keyof any, V> = { [P in K]: V; }; type StringMap = MyRecord<string, number>; // { [key: string]: number } type ThreeFlags = MyRecord<'a' | 'b' | 'c', boolean>; // { a: boolean; b: boolean; c: boolean; }Expected result
StringMap is an index signature; ThreeFlags has exactly three boolean keys.
- 5
Build DeepPartial
DeepPartial recursively makes all properties optional, including nested objects. Use a conditional to check if the value is an object.
Codetype DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]; }; interface Config { server: { port: number; host: string; }; db: { url: string; pool: number; }; } type PartialConfig = DeepPartial<Config>; // { server?: { port?: number; host?: string; }; db?: { url?: string; pool?: number; }; } const cfg: PartialConfig = { server: { port: 3000 } }; // OK — deeply partialExpected result
PartialConfig allows any level of partial nesting.
Try it yourself
type MyPartial<T> = /* TODO */;
type MyReadonly<T> = /* TODO */;
type MyPick<T, K> = /* TODO */;type MyPartial<T> = { [K in keyof T]?: T[K]; };
type DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]; };What you learned
You rebuilt TypeScript's built-in utility types from scratch — understanding them lets you create custom transformations for any object shape.
Related Resources
Related Lessons
- Mapped Types
[K in keyof T] maps over object keys.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.