Lesson 42 of 55
15 mins readPython Mapped Types and Mapping Modifiers
In Plain English
Mapped types build on index signature syntax to iterate over keys (using [P in K]) and apply transformations like making fields optional, readonly, or nullable.
Core Rules to Remember

[P in keyof T]: Iterates across every property key in type T.

Modifiers: Add or strip readonly (+readonly / -readonly) and optionality (? / -?).
Live Interactive Example
Hit Run Code to see it liveMapping Over Keys with Modifiers
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: Mapped types iterate over keyof T with [P in keyof T], transforming each property according to specified modifiers.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Create a Nullable Mapped Type
Define type Nullable<T> = { [P in keyof T]: T[P] | null }.
Apply to interface User { name: string; age: number }.
Create obj: Nullable<User> = { name: null, age: 25 } and print `${obj.name} ${obj.age}`.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.