Lesson 4 of 8
Mapped Types - Transform Every Property
Mapped types iterate over the keys of a type and produce a new type with transformed properties. They are the engine behind `Partial`, `Readonly`, `Required`, and `Record`. Writing your own mapped types lets you express complex type transformations that no utility type covers.
Mapped Types - Custom Transformations
// ── Mapped type syntax: { [K in keyof T]: TransformedType } ──
type MyPartial<T> = {
[K in keyof T]?: T[K]; // ? makes every property optional
};
type MyReadonly<T> = {
readonly [K in keyof T]: T[K]; // readonly on every property
};
type Nullable<T> = {
[K in keyof T]: T[K] | null; // add null to every property
};
// ── Removing modifiers with - ─────────────────────────────────
type Mutable<T> = {
-readonly [K in keyof T]: T[K]; // -readonly removes readonly
};
type MyRequired<T> = {
[K in keyof T]-?: T[K]; // -? removes optional
};
// ── Combining mapped types ────────────────────────────────────
type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
};
// ── Renaming keys with "as" clause ────────────────────────────
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
// For Task:
type TaskGetters = Getters<Pick<Task, "title" | "status" | "priority">>;
// {
// getTitle: () => string;
// getStatus: () => TaskStatus;
// getPriority: () => Priority;
// }
// ── Filtering keys with conditional in mapped type ─────────────
// Only string-valued keys:
type StringOnlyKeys<T> = {
[K in keyof T as T[K] extends string ? K : never]: T[K];
};
type TaskStringProps = StringOnlyKeys<Task>;
// { title: string; status: ...; } - only string/string-union props
// ── Practical: Validation schema from domain type ─────────────
type ValidationRule<T> = {
required?: boolean;
minLength?: T extends string ? number : never;
min?: T extends number ? number : never;
max?: T extends number ? number : never;
};
type TaskValidationSchema = {
[K in keyof CreateTaskInput]: ValidationRule<CreateTaskInput[K]>;
};
const taskSchema: TaskValidationSchema = {
title: { required: true, minLength: 3 },
description: { required: false },
priority: { required: true },
status: { required: true },
// ... (all CreateTaskInput fields with rules)
};Common Mistakes
- Forgetting `string &` constraint when using key in template literal - `Capitalize<K>` requires `K extends string`. Write `Capitalize<string & K>` to safely extract the string constraint from a `keyof` union that might also include `number | symbol`.
- Applying mapped types to union types - `{ [K in 'a' | 'b']: string }` creates `{ a: string; b: string }`, not a union. Mapped types always produce objects.
- Not testing mapped type outputs - open TypeScript playground or your IDE's `type X = YourMappedType<Z>` and hover to confirm the result is what you expect before using in production.
Tip
Tip
Practice Mapped Types Transform Every Property in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mapped Types - Transform Every Property
Practice Task
Note
Practice Task - (1) Write a working example of Mapped Types Transform Every Property from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mapped Types Transform Every Property is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- Mapped types iterate over the keys of a type and produce a new type with transformed properties.
- Forgetting `string &` constraint when using key in template literal - `Capitalize<K>` requires `K extends string`. Write `Capitalize<string & K>` to safely extract the string constraint from a `keyof` union that might also include `number | symbol`.
- Applying mapped types to union types - `{ [K in 'a' | 'b']: string }` creates `{ a: string; b: string }`, not a union. Mapped types always produce objects.
- Not testing mapped type outputs - open TypeScript playground or your IDE's `type X = YourMappedType<Z>` and hover to confirm the result is what you expect before using in production.
Finished this lesson?