Challenge - 5 Problems
Required 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 TypeScript code using Required type?
Consider the following TypeScript code that uses the
Required utility type. What will be the output when this code runs?Typescript
interface User {
id?: number;
name?: string;
}
function printUser(user: Required<User>) {
console.log(`ID: ${user.id}, Name: ${user.name}`);
}
const user = { id: 1, name: "Alice" };
printUser(user);Attempts:
2 left
💡 Hint
Think about what the Required type does to optional properties.
✗ Incorrect
The Required type makes all optional properties required. Since the object passed has both properties, the function prints their values correctly.
❓ Predict Output
intermediate2:00remaining
What error does this code raise when using Required type incorrectly?
Look at this TypeScript code snippet. What error will it produce?
Typescript
interface Config {
url?: string;
timeout?: number;
}
const config: Required<Config> = { url: "http://example.com" };Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
✗ Incorrect
Since Required makes all properties required, missing 'timeout' causes a compile-time error.
🔧 Debug
advanced2:00remaining
Why does this function cause a TypeScript error with Required type?
This function expects a parameter of type Required. Why does the following code cause a TypeScript error?
Typescript
interface Person {
name?: string;
age?: number;
}
function greet(person: Required<Person>) {
console.log(`Hello, ${person.name}!`);
}
greet({ name: "Bob" });Attempts:
2 left
💡 Hint
Required makes all properties mandatory, so missing any causes error.
✗ Incorrect
The argument lacks the 'age' property, which is required by Required, causing a compile-time error.
🧠 Conceptual
advanced1:30remaining
What does the Required type do in TypeScript?
Choose the best description of what the Required utility type does.
Attempts:
2 left
💡 Hint
Think about the word 'Required'.
✗ Incorrect
Required transforms all optional properties of T into required properties.
❓ Predict Output
expert2:30remaining
What is the output of this TypeScript code using Required with nested types?
Analyze this TypeScript code. What will it print when run?
Typescript
interface Address {
street?: string;
city?: string;
}
interface Employee {
name?: string;
address?: Address;
}
function printEmployee(emp: Required<Employee>) {
console.log(`Name: ${emp.name}, Street: ${emp.address.street}, City: ${emp.address.city}`);
}
const emp = {
name: "Eve",
address: {
street: "123 Main St",
city: "Wonderland"
}
};
printEmployee(emp);Attempts:
2 left
💡 Hint
Required only applies to the top-level properties, not nested ones.
✗ Incorrect
Required makes 'name' and 'address' required, but does not affect nested Address properties which remain optional. Since the object provides all nested properties, the output prints all values.