0
0
Typescriptprogramming~20 mins

Object type annotation inline in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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' }));
A"Hello, Alice, age 0"
B"Hello, Alice, age undefined"
CTypeError at runtime
D"Hello, Alice"
Attempts:
2 left
💡 Hint
Look at how the optional property age is checked before use.
Predict Output
intermediate
2: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' } }));
A"John Doe"
B"[object Object]"
CTypeError: Cannot read property 'first' of undefined
D"John undefined"
Attempts:
2 left
💡 Hint
Check how nested object properties are accessed.
🔧 Debug
advanced
2: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' });
AError: Property 'age' is missing in type '{ name: string; }' but required in type '{ name: string; age: number; }'.
BRuntime error: Cannot read property 'age' of undefined
CNo error, prints: Name: Bob, Age: undefined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check if all required properties are provided in the argument.
Predict Output
advanced
2: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' }));
A"User undefined: Eve"
BTypeError: Cannot assign to read only property 'id'
C"User 5: Eve"
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Readonly means the property cannot be changed after creation.
Predict Output
expert
2: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 }));
ATypeError: scores[key] is not a number
B45
CNaN
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Index signature allows any string key with number values.