Skip to content

Answer:47 #1336

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
56 changes: 21 additions & 35 deletions apps/typescript/47-enums-vs-union-types/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { Component, computed, signal } from '@angular/core';

enum Difficulty {
EASY = 'easy',
NORMAL = 'normal',
}
type Difficulty = keyof typeof difficulties;
const difficulties = {
easy: 'Easy',
normal: 'Normal',
} as const;

enum Direction {
LEFT = 'left',
RIGHT = 'right',
}
type Direction = 'left' | 'right';
type DirectionMap = { [K in Direction]: string };
const directions: DirectionMap = {
left: 'left (←)',
right: 'right (→)',
} as const;

@Component({
imports: [],
selector: 'app-root',
template: `
<section>
<div>
<button mat-stroked-button (click)="difficulty.set(Difficulty.EASY)">
<button mat-stroked-button (click)="difficulty.set('easy')">
Easy
</button>
<button mat-stroked-button (click)="difficulty.set(Difficulty.NORMAL)">
<button mat-stroked-button (click)="difficulty.set('normal')">
Normal
</button>
</div>
Expand All @@ -28,10 +30,8 @@ enum Direction {

<section>
<div>
<button mat-stroked-button (click)="direction.set(Direction.LEFT)">
Left
</button>
<button mat-stroked-button (click)="direction.set(Direction.RIGHT)">
<button mat-stroked-button (click)="direction.set('left')">Left</button>
<button mat-stroked-button (click)="direction.set('right')">
Right
</button>
</div>
Expand All @@ -53,30 +53,16 @@ enum Direction {
`,
})
export class AppComponent {
readonly Difficulty = Difficulty;
readonly difficulty = signal<Difficulty>(Difficulty.EASY);
readonly difficulty = signal<Difficulty>('easy');

readonly Direction = Direction;
readonly direction = signal<Direction | undefined>(undefined);

readonly difficultyLabel = computed<string>(() => {
switch (this.difficulty()) {
case Difficulty.EASY:
return Difficulty.EASY;
case Difficulty.NORMAL:
return Difficulty.NORMAL;
}
});
readonly difficultyLabel = computed<string>(
() => difficulties[this.difficulty()],
);

readonly directionLabel = computed<string>(() => {
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!';
}
const dir = this.direction();
return dir ? `You chose to go ${directions[dir]}` : 'Choose a direction';
});
}