0
0
Typescriptprogramming~20 mins

Required type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Required Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
ATypeScript compile error because user properties are optional
BID: undefined, Name: undefined
CID: 1, Name: Alice
DRuntime error because properties are missing
Attempts:
2 left
💡 Hint
Think about what the Required type does to optional properties.
Predict Output
intermediate
2: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" };
ATypeScript error: Property 'timeout' is missing
BNo error, code runs fine
CRuntime error: timeout is undefined
DTypeScript error: url property is optional
Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
🔧 Debug
advanced
2: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" });
AError because 'name' property is optional
BNo error, function runs and prints 'Hello, Bob!'
CRuntime error because 'age' is undefined
DError because 'age' property is missing in the argument
Attempts:
2 left
💡 Hint
Required makes all properties mandatory, so missing any causes error.
🧠 Conceptual
advanced
1:30remaining
What does the Required type do in TypeScript?
Choose the best description of what the Required utility type does.
AIt makes all properties of T readonly.
BIt makes all properties of T required (non-optional).
CIt removes all properties from T.
DIt makes all properties of T optional.
Attempts:
2 left
💡 Hint
Think about the word 'Required'.
Predict Output
expert
2: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);
AName: Eve, Street: 123 Main St, City: Wonderland
BTypeScript error: address properties are optional
CRuntime error: Cannot read property 'street' of undefined
DName: Eve, Street: undefined, City: undefined
Attempts:
2 left
💡 Hint
Required only applies to the top-level properties, not nested ones.