0
0
Typescriptprogramming~10 mins

Nested object types in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a nested object type with a property 'address'.

Typescript
type User = {
  name: string;
  address: [1];
};
Drag options to blanks, or click blank then click option'
A{ street: string; city: string }
Bnumber
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a simple type like string instead of an object type for address.
2fill in blank
medium

Complete the code to access the nested property 'city' from the user object.

Typescript
const user: User = {
  name: 'Alice',
  address: { street: 'Main St', city: 'Wonderland' }
};

const cityName = user.address.[1];
Drag options to blanks, or click blank then click option'
Acity
Baddress
Cname
Dstreet
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access a property that doesn't exist at that level.
Using the wrong property name.
3fill in blank
hard

Fix the error in the nested object type by completing the type for 'company'.

Typescript
type Employee = {
  id: number;
  company: [1];
};
Drag options to blanks, or click blank then click option'
Astring
B{ name: string; location: string }
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a primitive type like string instead of an object type for company.
4fill in blank
hard

Fill both blanks to define a nested object type with an array of objects.

Typescript
type Book = {
  title: string;
  author: [1];
};

type Library = {
  name: string;
  books: [2][];
};
Drag options to blanks, or click blank then click option'
ABook
Bstring
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a primitive type for books instead of an array of objects.
Using wrong type for author.
5fill in blank
hard

Fill all three blanks to define a nested object type with optional and nested properties.

Typescript
type Profile = {
  username: string;
  contact: {
    email: [1];
    phone?: [2];
  };
  preferences: [3];
};
Drag options to blanks, or click blank then click option'
Astring
Bnumber
C{ theme: string; notifications: boolean }
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for email or phone.
Not defining preferences as an object.