diff --git a/05_typescript/uebungen/u11_literals/index.html b/05_typescript/uebungen/u11_literals/index.html new file mode 100644 index 0000000..9bb81a6 --- /dev/null +++ b/05_typescript/uebungen/u11_literals/index.html @@ -0,0 +1,25 @@ + + + + + + Übung 11: Arbeiten mit Literal Types in TypeScript + + + + +
+
+
+

Übung 11: Arbeiten mit Literal Types in TypeScript

+

+ In dieser Übung übst du die Verwendung von Literal Types in TypeScript, um spezifischere und kontrollierte + Typen für deine Variablen und Funktionsparameter zu erstellen. Du wirst Funktionen definieren, die bestimmte + String-, numerische und boolesche Literal Types akzeptieren und sicherstellen, dass nur die erlaubten Werte + an diese Funktionen übergeben werden können. +

+
+
+
+ + diff --git a/05_typescript/uebungen/u11_literals/package.json b/05_typescript/uebungen/u11_literals/package.json new file mode 100644 index 0000000..285e9ca --- /dev/null +++ b/05_typescript/uebungen/u11_literals/package.json @@ -0,0 +1,16 @@ +{ + "name": "11_literals", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "ts": "npx esbuild src/main.ts --watch --bundle --outfile=assets/js/bundle.js --loader:.ts=ts" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "devDependencies": { + "esbuild": "^0.28.2" + } +} diff --git a/05_typescript/uebungen/u11_literals/src/main.ts b/05_typescript/uebungen/u11_literals/src/main.ts new file mode 100644 index 0000000..468d4b5 --- /dev/null +++ b/05_typescript/uebungen/u11_literals/src/main.ts @@ -0,0 +1,30 @@ +// [ ] Step 1: Create a function 'setStatus' that accepts a string literal type parameter 'status'. +// - The 'status' can only be "success", "error", or "loading". +// - The function should log the status to the console. + +// [ ] Step 2: Create a function 'rateExperience' that accepts a numeric literal type parameter 'rating'. +// - The 'rating' can only be 1, 2, 3, 4, or 5. +// - The function should return a message indicating the rating. + +// [ ] Step 3: Create a function 'togglePower' that accepts a boolean literal type parameter 'state'. +// - The 'state' can only be true or false. +// - The function should return a message indicating whether the power is ON or OFF. + +// [ ] Step 4: Test the functions with valid and invalid values to ensure the correct behavior and type safety. + +// Test cases (students should verify that these work as expected) + +// Test 'setStatus' function +setStatus('success'); // OK +setStatus('error'); // OK +setStatus('complete'); // Error: Argument of type '"complete"' is not assignable to parameter of type '"success" | "error" | "loading"'. + +// Test 'rateExperience' function +console.log(rateExperience(5)); // => "You rated the experience as 5" +console.log(rateExperience(3)); // => "You rated the experience as 3" +console.log(rateExperience(6)); // Error: Argument of type '6' is not assignable to parameter of type '1 | 2 | 3 | 4 | 5'. + +// Test 'togglePower' function +console.log(togglePower(true)); // => "Power is ON" +console.log(togglePower(false)); // => "Power is OFF" +console.log(togglePower('on')); // Error: Argument of type '"on"' is not assignable to parameter of type 'true | false'. diff --git a/05_typescript/uebungen/uebungen-04-10-ts.zip b/05_typescript/uebungen/uebungen-04-09-ts.zip similarity index 61% rename from 05_typescript/uebungen/uebungen-04-10-ts.zip rename to 05_typescript/uebungen/uebungen-04-09-ts.zip index a4a7de5..ac11f92 100644 Binary files a/05_typescript/uebungen/uebungen-04-10-ts.zip and b/05_typescript/uebungen/uebungen-04-09-ts.zip differ diff --git a/05_typescript/uebungen/uebungen-10-11-ts.zip b/05_typescript/uebungen/uebungen-10-11-ts.zip new file mode 100644 index 0000000..b6911f0 Binary files /dev/null and b/05_typescript/uebungen/uebungen-10-11-ts.zip differ