Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions book-content/chapters/06-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ To solve this challenge, we'll create a new `BaseEntity` type with the common pr
```typescript
type BaseEntity = {
id: string;
name: string;
createdAt: Date;
};
```
Expand All @@ -348,14 +349,12 @@ Once the `BaseEntity` type is created, we can intersect it with the `User` and `
type User = {
id: string;
createdAt: Date;
name: string;
email: string;
} & BaseEntity;

type Product = {
id: string;
createdAt: Date;
name: string;
price: number;
} & BaseEntity;
```
Expand All @@ -364,12 +363,10 @@ Then, we can remove the common properties from `User` and `Product`:

```typescript
type User = {
name: string;
email: string;
} & BaseEntity;

type Product = {
name: string;
price: number;
} & BaseEntity;
```
Expand Down Expand Up @@ -411,24 +408,26 @@ interface Product extends BaseEntity {
}
```

For the extra credit, we can take this further by creating `WithId` and `WithCreatedAt` interfaces that represent objects with an `id` and `createdAt` property. Then, we can have `User` and `Product` extend from these interfaces by adding commas:
For the extra credit, we can take this further by creating `WithId`, `WithName` and `WithCreatedAt` interfaces that represent objects with an `id` and `createdAt` property. Then, we can have `User` and `Product` extend from these interfaces by adding commas:

```typescript
interface WithId {
id: string;
}

interface WithName {
name: string;
}

interface WithCreatedAt {
createdAt: Date;
}

interface User extends WithId, WithCreatedAt {
name: string;
interface User extends WithId, WithName, WithCreatedAt {
email: string;
}

interface Product extends WithId, WithCreatedAt {
name: string;
interface Product extends WithId, WithName, WithCreatedAt {
price: number;
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { Extends, Expect } from "@total-typescript/helpers";
type BaseEntity = {
id: string;
createdAt: Date;
name: string;
};

type User = {
name: string;
email: string;
} & BaseEntity;

type Product = {
name: string;
price: number;
} & BaseEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { Extends, Expect } from "@total-typescript/helpers";
type BaseEntity = {
id: string;
createdAt: Date;
name: string;
};

type User = {
name: string;
email: string;
} & BaseEntity;

type Product = {
name: string;
price: number;
} & BaseEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { Extends, Expect } from "@total-typescript/helpers";
interface BaseEntity {
id: string;
createdAt: Date;
name: string;
}

interface User extends BaseEntity {
name: string;
email: string;
}

interface Product extends BaseEntity {
name: string;
price: number;
}

Expand Down
10 changes: 6 additions & 4 deletions src/020-objects/082-extend-object-using-interfaces.solution.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ interface WithId {
id: string;
}

interface WithName {
name: string;
}

interface WithCreatedAt {
createdAt: Date;
}

interface User extends WithId, WithCreatedAt {
name: string;
interface User extends WithId, WithName, WithCreatedAt {
email: string;
}

interface Product extends WithId, WithCreatedAt {
name: string;
interface Product extends WithId, WithName, WithCreatedAt {
price: number;
}

Expand Down