Partial and Pick
Partial Pick
π¨βπΌ We need to create types for updating user data. Some fields should be
optional, and we only want to allow updating specific fields.
type Article = {
id: string
title: string
body: string
updatedAt: Date
}
type ArticlePatch = Partial<Pick<Article, 'title' | 'body'>>
π¨ Open
and:
- Create
PartialUserβ allUserproperties optional - Create
UserNameEmailβ onlynameandemail - Create
UserUpdateβ optional updates limited tonameand/oremail - Remove the
@ts-expect-erroronceUserUpdateexists - Export
updateUser
Observable behavior for the provided
user fixture:updateUser(user, { name: 'Alice Smith' })β name becomes'Alice Smith', email stays'alice@example.com', id stays'1'updateUser(user, { email: 'alice.smith@example.com' })β email updates, name stays'Alice'- Updating both fields at once also works
- Updating
idshould be a type error