scrimba
Learn TypeScript
Omit Utility Type
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Omit Utility Type
AboutCommentsNotes
Omit Utility Type
Expand for more info
index.ts
run
preview
console
type User = {
id: number
username: string
role: "member" | "contributor" | "admin"
}

type UpdatedUser = Partial<User>

let nextUserId = 1

const users: User[] = [
{ id: nextUserId++, username: "john_doe", role: "member" },
{ id: nextUserId++, username: "jane_smith", role: "contributor" }
];

function updateUser(id: number, updates: UpdatedUser) {
const foundUser = users.find(user => user.id === id)
if (!foundUser) {
console.error("User not found!")
return
}
Object.assign(foundUser, updates)
}

// updateUser(1, { username: "new_john_doe" });
// updateUser(4, { role: "contributor" });

function addNewUser(newUser: any): User {
// Create a new variable called `user`, add an `id` property to it
// and spread in all the properties of the `newUser` object. Think
// about how you should set the type for this `user` object.
// Push the new object to the `users` array, and return the object
// from the function at the end
}

// example usage:
addNewUser({ username: "joe_schmoe", role: "member" })

console.log(users)
Console
[
{id:
1
, username:
"john_doe"
, role:
"member"
}
,
{id:
2
, username:
"jane_smith"
, role:
"contributor"
}
,
{id:
3
, username:
"joe_schmoe"
, role:
"member"
}
]
,
/index.html
-7:00