From 0df92c0869700bf8b47d797d7a72e8b5b30b4e88 Mon Sep 17 00:00:00 2001 From: Pavel Glac Date: Thu, 18 Jul 2024 12:57:54 +0000 Subject: [PATCH] chore(objects): interface examples simplification --- book-content/chapters/06-objects.md | 17 ++++++++--------- ...xtend-object-using-intersections.solution.ts | 3 +-- ...82-extend-object-using-interfaces.problem.ts | 3 +-- ...extend-object-using-interfaces.solution.1.ts | 3 +-- ...extend-object-using-interfaces.solution.2.ts | 10 ++++++---- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/book-content/chapters/06-objects.md b/book-content/chapters/06-objects.md index 594f940..7cc10b5 100644 --- a/book-content/chapters/06-objects.md +++ b/book-content/chapters/06-objects.md @@ -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; } ``` diff --git a/src/020-objects/081-extend-object-using-intersections.solution.ts b/src/020-objects/081-extend-object-using-intersections.solution.ts index d383063..3d205f5 100644 --- a/src/020-objects/081-extend-object-using-intersections.solution.ts +++ b/src/020-objects/081-extend-object-using-intersections.solution.ts @@ -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; diff --git a/src/020-objects/082-extend-object-using-interfaces.problem.ts b/src/020-objects/082-extend-object-using-interfaces.problem.ts index d383063..3d205f5 100644 --- a/src/020-objects/082-extend-object-using-interfaces.problem.ts +++ b/src/020-objects/082-extend-object-using-interfaces.problem.ts @@ -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; diff --git a/src/020-objects/082-extend-object-using-interfaces.solution.1.ts b/src/020-objects/082-extend-object-using-interfaces.solution.1.ts index 4f2cb76..93352b7 100644 --- a/src/020-objects/082-extend-object-using-interfaces.solution.1.ts +++ b/src/020-objects/082-extend-object-using-interfaces.solution.1.ts @@ -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; } diff --git a/src/020-objects/082-extend-object-using-interfaces.solution.2.ts b/src/020-objects/082-extend-object-using-interfaces.solution.2.ts index 36d168d..d936bb1 100644 --- a/src/020-objects/082-extend-object-using-interfaces.solution.2.ts +++ b/src/020-objects/082-extend-object-using-interfaces.solution.2.ts @@ -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; }