Challenge - 5 Problems
Utility Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using Partial utility type?
Consider this TypeScript code snippet using the Partial utility type. What will be the output when running the compiled JavaScript?
Typescript
interface User {
name: string;
age: number;
}
function updateUser(user: User, fieldsToUpdate: Partial<User>): User {
return { ...user, ...fieldsToUpdate };
}
const user1: User = { name: "Alice", age: 30 };
const updatedUser = updateUser(user1, { age: 31 });
console.log(updatedUser);Attempts:
2 left
💡 Hint
Partial allows updating only some properties of an object.
✗ Incorrect
The Partial utility type makes all properties optional, so you can pass only the fields you want to update. The function merges the original user with the updated fields, resulting in the age being updated to 31 while keeping the name unchanged.
🧠 Conceptual
intermediate1:30remaining
Why are utility types like Readonly useful in TypeScript?
Which of the following best explains why the Readonly utility type is needed in TypeScript?
Attempts:
2 left
💡 Hint
Think about protecting data from being changed after creation.
✗ Incorrect
Readonly makes all properties of a type immutable, which helps prevent bugs caused by accidental modification of objects that should not change.
❓ Predict Output
advanced2:00remaining
What is the output of this TypeScript code using Record utility type?
Given this TypeScript code using the Record utility type, what will be printed to the console?
Typescript
type Roles = "admin" | "user" | "guest"; const rolePermissions: Record<Roles, string[]> = { admin: ["read", "write", "delete"], user: ["read", "write"], guest: ["read"] }; console.log(rolePermissions.user.includes("delete"));
Attempts:
2 left
💡 Hint
Check if the 'user' role has 'delete' permission in the array.
✗ Incorrect
The 'user' role has permissions ['read', 'write'], so 'delete' is not included, resulting in false.
🔧 Debug
advanced1:30remaining
What error does this TypeScript code produce?
This code tries to use the Required utility type. What error will it cause?
Typescript
interface Config {
url?: string;
port?: number;
}
const config: Required<Config> = {
url: "http://example.com"
};Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
✗ Incorrect
Required converts all optional properties to required, so 'port' must be provided. Missing it causes a compile-time error.
🚀 Application
expert2:00remaining
How many keys are in the resulting object after using Pick utility type?
Given this TypeScript code, how many keys does the object 'picked' have?
Typescript
interface Person {
name: string;
age: number;
location: string;
email: string;
}
const person: Person = {
name: "Bob",
age: 25,
location: "NY",
email: "bob@example.com"
};
const picked: Pick<Person, "name" | "email"> = {
name: person.name,
email: person.email
};Attempts:
2 left
💡 Hint
Pick selects only specified keys from a type.
✗ Incorrect
Pick creates a type with only 'name' and 'email' keys, so the object has 2 keys.