From 61efab51eb74c622892ddadebb810289a2560fdf Mon Sep 17 00:00:00 2001 From: Michal Grzegorczyk Date: Sun, 6 Apr 2025 18:56:57 +0200 Subject: [PATCH] feat(47): finished --- .../src/app/app.component.ts | 49 +++++-------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/apps/typescript/47-enums-vs-union-types/src/app/app.component.ts b/apps/typescript/47-enums-vs-union-types/src/app/app.component.ts index 05886724f..1e5ab8b53 100644 --- a/apps/typescript/47-enums-vs-union-types/src/app/app.component.ts +++ b/apps/typescript/47-enums-vs-union-types/src/app/app.component.ts @@ -1,14 +1,7 @@ import { Component, computed, signal } from '@angular/core'; -enum Difficulty { - EASY = 'easy', - NORMAL = 'normal', -} - -enum Direction { - LEFT = 'left', - RIGHT = 'right', -} +type Difficulty = { [K in 'EASY' | 'NORMAL']: string }; +type Direction = { [K in 'LEFT' | 'RIGHT']: string }; @Component({ imports: [], @@ -16,10 +9,10 @@ enum Direction { template: `
- -
@@ -28,10 +21,8 @@ enum Direction {
- - +
@@ -53,30 +44,16 @@ enum Direction { `, }) export class AppComponent { - readonly Difficulty = Difficulty; - readonly difficulty = signal(Difficulty.EASY); + readonly difficulty = signal('EASY'); + readonly direction = signal(undefined); - readonly Direction = Direction; - readonly direction = signal(undefined); - - readonly difficultyLabel = computed(() => { - switch (this.difficulty()) { - case Difficulty.EASY: - return Difficulty.EASY; - case Difficulty.NORMAL: - return Difficulty.NORMAL; - } - }); + readonly difficultyLabel = computed(() => this.difficulty()); readonly directionLabel = computed(() => { - const prefix = 'You chose to go'; - switch (this.direction()) { - case Direction.LEFT: - return `${prefix} ${Direction.LEFT}`; - case Direction.RIGHT: - return `${prefix} ${Direction.RIGHT}`; - default: - return 'Choose a direction!'; + if (!this.direction()) { + return 'Choose a direction!'; } + + return `You chose to go ${this.direction()}`; }); }