[Typescript] “Partial” Mapped Type

type MyPartial<T> = {
    [P in keyof T]?: T[P]
}

'in' like a loop. 

 

Usage:

function updatePerson(person: Person, prop: MyPartial<Person>) {
    return {...person, ...prop}
}

 

or built-in:

function updatePerson(person: Person, prop: Partial<Person>) {
    return {...person, ...prop}
}

 

posted @ 2020-10-01 18:42  Zhentiw  阅读(95)  评论(0编辑  收藏  举报