mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 21:03:03 -05:00
feat(challenge 47): typescript enums vs union types
This commit is contained in:
36
apps/typescript/enums-vs-union-types/.eslintrc.json
Normal file
36
apps/typescript/enums-vs-union-types/.eslintrc.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"extends": ["../../../.eslintrc.json"],
|
||||
"ignorePatterns": ["!**/*"],
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["*.ts"],
|
||||
"extends": [
|
||||
"plugin:@nx/angular",
|
||||
"plugin:@angular-eslint/template/process-inline-templates"
|
||||
],
|
||||
"rules": {
|
||||
"@angular-eslint/directive-selector": [
|
||||
"error",
|
||||
{
|
||||
"type": "attribute",
|
||||
"prefix": "app",
|
||||
"style": "camelCase"
|
||||
}
|
||||
],
|
||||
"@angular-eslint/component-selector": [
|
||||
"error",
|
||||
{
|
||||
"type": "element",
|
||||
"prefix": "app",
|
||||
"style": "kebab-case"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": ["*.html"],
|
||||
"extends": ["plugin:@nx/angular-template"],
|
||||
"rules": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
13
apps/typescript/enums-vs-union-types/README.md
Normal file
13
apps/typescript/enums-vs-union-types/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Enums vs Union Types
|
||||
|
||||
> author: thomas-laforge
|
||||
|
||||
### Run Application
|
||||
|
||||
```bash
|
||||
npx nx serve typescript-enums-vs-union-types
|
||||
```
|
||||
|
||||
### Documentation and Instruction
|
||||
|
||||
Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/typescript/47-enums-vs-union-types/).
|
||||
73
apps/typescript/enums-vs-union-types/project.json
Normal file
73
apps/typescript/enums-vs-union-types/project.json
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"name": "typescript-enums-vs-union-types",
|
||||
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
||||
"projectType": "application",
|
||||
"prefix": "app",
|
||||
"sourceRoot": "apps/typescript/enums-vs-union-types/src",
|
||||
"tags": [],
|
||||
"targets": {
|
||||
"build": {
|
||||
"executor": "@angular-devkit/build-angular:application",
|
||||
"outputs": ["{options.outputPath}"],
|
||||
"options": {
|
||||
"outputPath": "dist/apps/typescript/enums-vs-union-types",
|
||||
"index": "apps/typescript/enums-vs-union-types/src/index.html",
|
||||
"browser": "apps/typescript/enums-vs-union-types/src/main.ts",
|
||||
"polyfills": ["zone.js"],
|
||||
"tsConfig": "apps/typescript/enums-vs-union-types/tsconfig.app.json",
|
||||
"inlineStyleLanguage": "scss",
|
||||
"assets": [
|
||||
"apps/typescript/enums-vs-union-types/src/favicon.ico",
|
||||
"apps/typescript/enums-vs-union-types/src/assets"
|
||||
],
|
||||
"styles": ["apps/typescript/enums-vs-union-types/src/styles.scss"],
|
||||
"scripts": []
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kb",
|
||||
"maximumError": "1mb"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "2kb",
|
||||
"maximumError": "4kb"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
},
|
||||
"development": {
|
||||
"optimization": false,
|
||||
"extractLicenses": false,
|
||||
"sourceMap": true
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "production"
|
||||
},
|
||||
"serve": {
|
||||
"executor": "@angular-devkit/build-angular:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"buildTarget": "typescript-enums-vs-union-types:build:production"
|
||||
},
|
||||
"development": {
|
||||
"buildTarget": "typescript-enums-vs-union-types:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"executor": "@angular-devkit/build-angular:extract-i18n",
|
||||
"options": {
|
||||
"buildTarget": "typescript-enums-vs-union-types:build"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"executor": "@nx/eslint:lint",
|
||||
"outputs": ["{options.outputFile}"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { Component, computed, signal } from '@angular/core';
|
||||
|
||||
enum Difficulty {
|
||||
EASY = 'easy',
|
||||
NORMAL = 'normal',
|
||||
}
|
||||
|
||||
enum Direction {
|
||||
LEFT = 'left',
|
||||
RIGHT = 'right',
|
||||
}
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [],
|
||||
selector: 'app-root',
|
||||
template: `
|
||||
<section>
|
||||
<div>
|
||||
<button mat-stroked-button (click)="difficulty.set(Difficulty.EASY)">
|
||||
Easy
|
||||
</button>
|
||||
<button mat-stroked-button (click)="difficulty.set(Difficulty.NORMAL)">
|
||||
Normal
|
||||
</button>
|
||||
</div>
|
||||
<p>Selected Difficulty: {{ difficultyLabel() }}</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<div>
|
||||
<button mat-stroked-button (click)="direction.set(Direction.LEFT)">
|
||||
Left
|
||||
</button>
|
||||
<button mat-stroked-button (click)="direction.set(Direction.RIGHT)">
|
||||
Right
|
||||
</button>
|
||||
</div>
|
||||
<p>{{ directionLabel() }}</p>
|
||||
</section>
|
||||
`,
|
||||
styles: `
|
||||
section {
|
||||
@apply flex flex-col mx-auto my-5 w-fit gap-2 items-center;
|
||||
|
||||
> div {
|
||||
@apply flex w-fit gap-5;
|
||||
}
|
||||
}
|
||||
|
||||
button {
|
||||
@apply rounded-md border px-4 py-2;
|
||||
}
|
||||
`,
|
||||
})
|
||||
export class AppComponent {
|
||||
readonly Difficulty = Difficulty;
|
||||
readonly difficulty = signal<Difficulty>(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 directionLabel = computed<string>(() => {
|
||||
const prefix = 'You choosed to go';
|
||||
switch (this.direction()) {
|
||||
case Direction.LEFT:
|
||||
return `${prefix} ${Direction.LEFT}`;
|
||||
case Direction.RIGHT:
|
||||
return `${prefix} ${Direction.RIGHT}`;
|
||||
default:
|
||||
return 'Choose a direction!';
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [],
|
||||
};
|
||||
BIN
apps/typescript/enums-vs-union-types/src/favicon.ico
Normal file
BIN
apps/typescript/enums-vs-union-types/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/typescript/enums-vs-union-types/src/index.html
Normal file
13
apps/typescript/enums-vs-union-types/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>typescript-enums-vs-union-types</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
7
apps/typescript/enums-vs-union-types/src/main.ts
Normal file
7
apps/typescript/enums-vs-union-types/src/main.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { AppComponent } from './app/app.component';
|
||||
import { appConfig } from './app/app.config';
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig).catch((err) =>
|
||||
console.error(err),
|
||||
);
|
||||
5
apps/typescript/enums-vs-union-types/src/styles.scss
Normal file
5
apps/typescript/enums-vs-union-types/src/styles.scss
Normal file
@@ -0,0 +1,5 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
14
apps/typescript/enums-vs-union-types/tailwind.config.js
Normal file
14
apps/typescript/enums-vs-union-types/tailwind.config.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const { createGlobPatternsForDependencies } = require('@nx/angular/tailwind');
|
||||
const { join } = require('path');
|
||||
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: [
|
||||
join(__dirname, 'src/**/!(*.stories|*.spec).{ts,html}'),
|
||||
...createGlobPatternsForDependencies(__dirname),
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
10
apps/typescript/enums-vs-union-types/tsconfig.app.json
Normal file
10
apps/typescript/enums-vs-union-types/tsconfig.app.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../../dist/out-tsc",
|
||||
"types": []
|
||||
},
|
||||
"files": ["src/main.ts"],
|
||||
"include": ["src/**/*.d.ts"],
|
||||
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src/**/*.ts"],
|
||||
"compilerOptions": {
|
||||
"types": []
|
||||
}
|
||||
}
|
||||
30
apps/typescript/enums-vs-union-types/tsconfig.json
Normal file
30
apps/typescript/enums-vs-union-types/tsconfig.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"useDefineForClassFields": false,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
},
|
||||
"files": [],
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.app.json",
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.editor.json",
|
||||
},
|
||||
],
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true,
|
||||
"strictTemplates": true,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user