0
0
Typescriptprogramming~20 mins

Nested object types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested object property access
What is the output of the following TypeScript code?
Typescript
type User = {
  id: number;
  profile: {
    name: string;
    address: {
      city: string;
      zip: string;
    };
  };
};

const user: User = {
  id: 1,
  profile: {
    name: "Alice",
    address: {
      city: "Wonderland",
      zip: "12345"
    }
  }
};

console.log(user.profile.address.city);
Aundefined
B"12345"
C"Wonderland"
DTypeError at runtime
Attempts:
2 left
💡 Hint
Look at how the nested objects are accessed using dot notation.
Predict Output
intermediate
2:00remaining
Output of nested object with optional chaining
What will this TypeScript code print?
Typescript
type Product = {
  id: number;
  details?: {
    name: string;
    manufacturer?: {
      name: string;
    };
  };
};

const product: Product = {
  id: 101
};

console.log(product.details?.manufacturer?.name ?? "Unknown");
A"" (empty string)
Bundefined
CTypeError at runtime
D"Unknown"
Attempts:
2 left
💡 Hint
Optional chaining returns undefined if any part is missing, then the nullish coalescing operator provides a default.
🔧 Debug
advanced
2:00remaining
Identify the error in nested object type assignment
What error does this TypeScript code produce?
Typescript
type Config = {
  server: {
    host: string;
    port: number;
  };
};

const config: Config = {
  server: {
    host: "localhost",
    port: "8080"
  }
};
ANo error, code compiles fine
BType 'string' is not assignable to type 'number' for 'port'
CProperty 'server' is missing in object
DType 'number' is not assignable to type 'string' for 'host'
Attempts:
2 left
💡 Hint
Check the type of 'port' in the object vs the type definition.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a nested object type?
Which of the following TypeScript type definitions correctly defines a nested object type for a 'Book' with 'title' (string) and 'author' (object with 'name' and 'age')?
Atype Book = { title: string; author: { name: string; age: number } };
Btype Book = { title: string; author: [name: string, age: number] };
Ctype Book = { title: string; author: (name: string, age: number) };
Dtype Book = { title: string; author: string & number };
Attempts:
2 left
💡 Hint
Look for the correct syntax for nested object types in TypeScript.
🚀 Application
expert
3:00remaining
Calculate total price from nested order object
Given the following nested order object, what is the value of 'totalPrice' after running the code?
Typescript
type Item = { name: string; price: number; quantity: number };

type Order = {
  id: number;
  items: Item[];
};

const order: Order = {
  id: 555,
  items: [
    { name: "Pen", price: 1.5, quantity: 10 },
    { name: "Notebook", price: 3, quantity: 5 },
    { name: "Eraser", price: 0.5, quantity: 20 }
  ]
};

const totalPrice = order.items.reduce((sum, item) => sum + item.price * item.quantity, 0);

console.log(totalPrice);
A40
B35
C45
D50
Attempts:
2 left
💡 Hint
Multiply price by quantity for each item and add all together.