Challenge - 5 Problems
Object Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inline object type annotation with optional property
What is the output of this TypeScript code when compiled and run with Node.js (ignoring type errors)?
Typescript
function greet(user: { name: string; age?: number }) { return `Hello, ${user.name}` + (user.age !== undefined ? `, age ${user.age}` : ''); } console.log(greet({ name: 'Alice' }));
Attempts:
2 left
💡 Hint
Look at how the optional property age is checked before use.
✗ Incorrect
The function checks if user.age is truthy before adding it to the string. Since age is undefined, it is skipped, so output is "Hello, Alice".
❓ Predict Output
intermediate2:00remaining
Output of inline object type annotation with nested object
What is the output of this TypeScript code?
Typescript
function getFullName(user: { name: { first: string; last: string } }) { return `${user.name.first} ${user.name.last}`; } console.log(getFullName({ name: { first: 'John', last: 'Doe' } }));
Attempts:
2 left
💡 Hint
Check how nested object properties are accessed.
✗ Incorrect
The function accesses first and last properties correctly from the nested name object, so it returns "John Doe".
🔧 Debug
advanced2:00remaining
Identify the error in inline object type annotation
What error will this TypeScript code produce?
Typescript
function printUser(user: { name: string; age: number }) { console.log(`Name: ${user.name}, Age: ${user.age}`); } printUser({ name: 'Bob' });
Attempts:
2 left
💡 Hint
Check if all required properties are provided in the argument.
✗ Incorrect
The age property is required but missing in the argument object, so TypeScript reports a compile-time error.
❓ Predict Output
advanced2:00remaining
Output of inline object type annotation with readonly property
What is the output of this TypeScript code?
Typescript
function updateUser(user: { readonly id: number; name: string }) { // user.id = 10; // Uncommenting this line causes error return `User ${user.id}: ${user.name}`; } console.log(updateUser({ id: 5, name: 'Eve' }));
Attempts:
2 left
💡 Hint
Readonly means the property cannot be changed after creation.
✗ Incorrect
The code does not try to change the readonly property, so it prints "User 5: Eve" without error.
❓ Predict Output
expert2:00remaining
Output of inline object type annotation with index signature
What is the output of this TypeScript code?
Typescript
function sumScores(scores: { [key: string]: number }) { let total = 0; for (const key in scores) { total += scores[key]; } return total; } console.log(sumScores({ math: 10, science: 20, english: 15 }));
Attempts:
2 left
💡 Hint
Index signature allows any string key with number values.
✗ Incorrect
The function sums all values in the object and returns 45.