Skip to content

chore(objects): interface examples simplification #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -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;
};
```
@@ -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;
```
@@ -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;
```
@@ -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;
}
```
Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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;
}

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
@@ -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;
}