Challenge - 5 Problems
Partial Type 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 code using Partial?
Consider the following TypeScript code using the Partial type. What will be logged to the console?
Typescript
interface User {
id: number;
name: string;
email: string;
}
function updateUser(user: User, updates: Partial<User>): User {
return { ...user, ...updates };
}
const user1: User = { id: 1, name: "Alice", email: "alice@example.com" };
const updatedUser = updateUser(user1, { email: "alice.new@example.com" });
console.log(updatedUser);Attempts:
2 left
💡 Hint
Partial allows you to provide some or all properties of the type.
✗ Incorrect
The Partial type means the updates object can have zero or more properties of User. The updateUser function merges the original user with the updates, so only the email changes.
❓ Predict Output
intermediate2:00remaining
What error does this code raise?
What error will this TypeScript code produce?
Typescript
interface Product {
id: number;
name: string;
price: number;
}
function setProductName(product: Partial<Product>, name: string) {
product.name = name;
}
const p: Partial<Product> = {};
setProductName(p, "Book");
console.log(p.name.length);Attempts:
2 left
💡 Hint
Objects are passed by reference in JavaScript/TypeScript, so the function mutates the original `p` object.
✗ Incorrect
No runtime error occurs. The `Partial` type makes properties optional, but the `setProductName` function mutates `p` by setting `p.name = "Book"`. Thus, `p.name.length` is 4.
🔧 Debug
advanced2:00remaining
Why does this Partial type assignment cause an error?
This code causes a TypeScript error. Why?
Typescript
interface Settings {
theme: string;
notifications: boolean;
}
const defaultSettings: Settings = { theme: "dark", notifications: true };
const newSettings: Partial<Settings> = { theme: undefined };
const finalSettings: Settings = { ...defaultSettings, ...newSettings };Attempts:
2 left
💡 Hint
Check the type of 'theme' in Settings and what value is assigned in newSettings.
✗ Incorrect
Partial makes properties optional but does not allow them to be explicitly undefined if the original type does not allow undefined. Setting theme to undefined causes a type error because Settings.theme expects a string, not undefined.
🧠 Conceptual
advanced2:00remaining
How does Partial affect nested objects?
Given the interface below, what does Partial do to the nested 'address' property?
Typescript
interface Address {
street: string;
city: string;
}
interface User {
name: string;
address: Address;
}
const partialUser: Partial<User> = { address: { city: "New York" } };Attempts:
2 left
💡 Hint
Partial only applies to the top-level properties of the type.
✗ Incorrect
Partial makes the 'address' property optional, but does not recursively make properties inside 'address' optional. So 'address' can be missing, but if present, it must have all Address properties.
🚀 Application
expert3:00remaining
How to create a recursive Partial type?
Which of the following TypeScript code snippets correctly defines a recursive Partial type that makes all nested properties optional?
Attempts:
2 left
💡 Hint
Recursive mapped types use conditional or mapped types to apply Partial deeply.
✗ Incorrect
Option B correctly defines DeepPartial recursively by making each property optional and applying DeepPartial to nested properties. Other options either combine types incorrectly or do not recurse properly.