Creating Mapped Types
Creating Mapped Types
π¨βπΌ Let's build our own utility types using mapped types!
type Album = {
title: string
year: number
}
type Optional<T> = { [K in keyof T]?: T[K] }
type OptionalAlbum = Optional<Album>
π¨ Open
and create:
MyPartial<T>β every property optionalMyRequired<T>β every property required (optional markers removed)Nullable<T>β every property becomesOriginal | nullMutable<T>β everyreadonlyproperty becomes writableStringify<T>β every property becomesstring
Then create and export these fixtures:
partial:{ name: 'Alice' }nullable:{ id: null, name: 'Bob', email: null, age: 30 }mutable: a full writable user object wheremutable.name = 'Updated'type-checks


