[TypeScript] "Pick" Mapped Type
interface Person { name: string; age: number; address: {} } // gives error since we didn't define 'address' const person: Person = { name: "Wan", age: 30 }
We can use 'Pick' mapped type to define a subtype, we can also write on our own
type MyPcik<T, K extends keyof T> = { [P in K]: T[P] } // K extends keyof T --> 'name' | 'age'
Usage:
const person: MyPick<Person, 'name' | 'age'> = { name: 'Wan', age: 30 }
Use built-in Pick:
const person: Pick<Person, 'name' | 'age'> = { name: 'Wan', age: 30 }