FAQ: Advanced Object Types - Deep Types

This community-built FAQ covers the “Deep Types” exercise from the lesson “Advanced Object Types”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn TypeScript

FAQs on the exercise Deep Types

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I thought that the constructor had to be the first thing you declared in a class, but here we have an about property that comes before it.

class OneSeries implements Robot {
  about;
 
  constructor(props: { general: { id: number; name: string; } }) {
    this.about = props;
  }
 
  getRobotId() {
    return `ID: ${this.about.general.id}`;
  }
}

Also, doesn’t the line this.about = props; make the first line redundant?

I am going through these lessons as well. I have found that trying to answer these questions also helps further my own understanding, so I am going to give this a shot! Please correct me if I am wrong!

I thought that the constructor had to be the first thing you declared in a class, but here we have an about property that comes before it.

What we see here is a class OneSeries that is having its type applied to it from the interface Robot with the implements keyword.

In order for this implementation to work, a class must adhere to the shape of the interface they implement from. In other words, a class must contain the same properties and methods as the interface. Additionally, a class can have properties and methods of their own.

Let’s continue to break this down…

The interface Robot has a property named about that is an object with a nested object, as well as a method, getRobotId().

The class OneSeries has a property named about that does not have a value yet, as well as two methods, constructor() and getRobotId().

Since we know that OneSeries implements its type from Robot, we know to expect the value for the about property in the class OneSeries must have the same shape as the about property in the interface Robot, which is to have the value of an object with a nested object with all matching properties.

Also, doesn’t the line this.about = props; make the first line redundant?

Inside the class OneSeries, the constructor() method has a parameter named props containing an object with a nested object that are the same shape as the about object in the interface Robot.

The constructor() method returns the expression this.about = props. The this keyword is referring to the class OneSeries because the constructor() method is a method of the class OneSeries. Therefore, appending the this keyword with the about property is simply referring to the about property on each and every object instance of the class OneSeries.

In the end, the return value of the constructor() method is saying set the about property equal to the props object.