Objects 16 exercises

Extend an Object Using Interfaces in TypeScript

We have a BaseEntity type, as well as User and Product types that intersect with it to bring in the id and createdAt properties:

type BaseEntity = {
id: string;
createdAt: Date;
type User = {
name: string;
email: string;
} & BaseEntity;
type Product = {

Loading exercise


00:00 We're starting this exercise just where we left off in the previous exercise, where we have our type, BaseEntity. We have our type, User, which is being intersected with BaseEntity in order to add the id and createdAt properties. And type, Product, is doing the same thing. It's being intersected with type, BaseEntity.

00:16 Our tests are making sure that User and Product have id, createdAt, name, and either price or email. So, this though, I would say, is not the most effective way to create objects that inherit from other objects in TypeScript.

00:32 There is a more effective way, and it involves the use of the interface keyword. Your job is to try to find out how you can refactor this code up here, these three types, to use interfaces instead. And instead of doing this intersection, we actually want it to inherit using something called interface extends.

00:51 I've given you all the information you need. Good luck.