Bird
0
0

Given two interfaces:

hard📝 Application Q9 of 15
Angular - TypeScript in Angular
Given two interfaces:

interface Address { street: string; city: string; }
interface User { name: string; address: Address; }

How would you create a valid User object with nested Address?
Aconst user: User = { name: 'John', address: { street: 'Main St', city: 'Townsville' } };
Bconst user: User = { name: 'John', address: 'Main St, Townsville' };
Cconst user: User = { name: 'John', address: null };
Dconst user: User = { name: 'John' };
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested interface structure

    The User interface requires an address property of type Address, which is an object with street and city.
  2. Step 2: Check object assignments

    const user: User = { name: 'John', address: { street: 'Main St', city: 'Townsville' } }; correctly assigns an object matching Address. Options B, C, and D do not match the required structure.
  3. Final Answer:

    const user: User = { name: 'John', address: { street: 'Main St', city: 'Townsville' } }; -> Option A
  4. Quick Check:

    Nested interfaces require matching nested objects [OK]
Quick Trick: Nested interfaces require nested objects with matching properties [OK]
Common Mistakes:
  • Assigning string instead of object for nested interface
  • Assigning null to required nested property
  • Omitting required nested property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes