This commit is contained in:
Philippe Torrel
2026-10-20 09:51:20 +02:00
parent 16416a0239
commit bcb589d15e
28 changed files with 657 additions and 69 deletions
+12
View File
@@ -68,6 +68,18 @@
- [SVG Flaggen](https://hampusborgos.github.io/country-flags/)
- [Flagicons](https://flagicons.lipis.dev/)
## Terminals
- [iterm - Unix](https://iterm2.com/) - RECOMMENDED
- [Ghostty - Unix](https://ghostty.org/)
- [Warp](https://www.warp.dev/) - ALL
- [Hyper.js](https://hyper.is/) - ALL
- [Ubuntu WSL](https://ubuntu.com/wsl) - WIN | UNIX
- [Windows Terminal](https://apps.microsoft.com/detail/9n0dx20hk701?icid=CNavAppsWindowsApps&hl=de-DE&gl=DE) - RECOMMENDED (WIN)
- [Git Bash](https://git-scm.com/)
- [Cmdr Terminal mit Bash](https://cmder.app/)
## Blog/ Techartikelseiten
- [Dev.to](https://dev.to/)
@@ -0,0 +1,32 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
var formatInput = (input) => {
if (typeof input === "number") {
return String(input * 100);
} else if (typeof input === "string") {
return input.toLowerCase();
} else if (typeof input === "boolean") {
return input ? "Yes" : "No";
}
};
console.log(formatInput(0.5));
console.log(formatInput(12));
console.log(formatInput("Hello"));
console.log(formatInput("WORLD"));
console.log(formatInput(true));
console.log(formatInput(false));
}
});
require_main();
})();
@@ -1,3 +1,33 @@
const formatInput = (input: number | string | boolean) => {
if (typeof input === 'number') {
return String(input * 100);
} else if (typeof input === 'string') {
return input.toLowerCase();
} else if (typeof input === 'boolean') {
return input ? 'Yes' : 'No';
}
};
const formatInput2 = (input: number | string | boolean) => {
let sReturn;
switch (typeof input) {
case 'number':
sReturn = input * 100;
break;
case 'string':
sReturn = input.toLowerCase();
break;
case 'boolean':
sReturn = input ? 'Yes' : 'No';
break;
default:
// nothing
}
return `formatierter string ${sReturn}`;
};
console.log(formatInput(0.5)); // => "50"
console.log(formatInput(12)); // => "1200"
console.log(formatInput('Hello')); // => "hello"
@@ -0,0 +1,29 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
var displayStatus = (status) => {
if (status) {
return "Status: " + status;
}
return "No status available.";
return status ? `Status: ${status}` : "No status available.";
};
console.log(displayStatus("Active"));
console.log(displayStatus("Pending"));
console.log(displayStatus(null));
console.log(displayStatus(void 0));
console.log(displayStatus(""));
}
});
require_main();
})();
@@ -1,17 +1,22 @@
// Übung 13: Implementierung der Truthiness Narrowing in einer Funktion
// Schreibe eine TypeScript-Funktion namens displayStatus, die einen Parameter status akzeptiert, der ein string, null oder undefined sein kann. Die Funktion sollte:
// Verwende die Truthiness Narrowing, um zu prüfen, ob der Status truthy oder falsch ist.
const displayStatus = (status: string | null | undefined): string => {
// Wenn status truthy ist, wird die Zeichenfolge "Status: ", verkettet mit Status.
if (status) {
return 'Status: ' + status;
}
// Wenn status fehlerhaft ist, wird die Zeichenfolge "No status available." zurückgegeben.
return 'No status available.';
return status ? `Status: ${status}` : 'No status available.';
};
// Kopiere den Code unten und füge ihn ein, um deine Implementierung zu testen.
console.log(displayStatus('Active')); // => "Status: Active"
console.log(displayStatus('Pending')); // => "Status: Pending"
console.log(displayStatus(null)); // => "No status available."
console.log(displayStatus(undefined)); // => "No status available."
console.log(displayStatus('')); // => "No status available."
// Übung 13: Implementierung der Truthiness Narrowing in einer Funktion
// Schreibe eine TypeScript-Funktion namens displayStatus, die einen Parameter status akzeptiert, der ein string, null oder undefined sein kann. Die Funktion sollte:
// Wenn status truthy ist, wird die Zeichenfolge "Status: ", verkettet mit Status.
// Wenn status fehlerhaft ist, wird die Zeichenfolge "No status available." zurückgegeben.
// Verwende die Truthiness Narrowing, um zu prüfen, ob der Status truthy oder falsch ist.
// Kopiere den Code unten und füge ihn ein, um deine Implementierung zu testen.
// // Kopiere den Code unten und füge ihn ein, um deine Implementierung zu testen.
@@ -0,0 +1,50 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
var analyzeInput = (input) => {
if (input === true) {
return "Input is exactly true";
} else if (input === false) {
return "Input is exactly false";
} else if (input === 0) {
return "Input is zero";
} else if (typeof input === "number") {
if (input > 0) {
return "Input is a positive number";
} else if (input < 0) {
return "Input is a negative number";
} else {
return "Unknown input";
}
} else if (input === "hello") {
return "Input is the string 'hello'";
} else if (typeof input === "string") {
return "Input is another string";
} else {
return "Unknown input";
}
};
console.log(analyzeInput(true));
console.log(analyzeInput(false));
console.log(analyzeInput(0));
console.log(analyzeInput(42));
console.log(analyzeInput(-7));
console.log(analyzeInput("hello"));
console.log(analyzeInput("world"));
console.log(analyzeInput(""));
console.log(analyzeInput(void 0));
console.log(analyzeInput(null));
}
});
require_main();
})();
@@ -1,7 +1,55 @@
const analyzeInput = (input: string | number | boolean): string => {
// Your code here
if (input === true) {
return 'Input is exactly true';
} else if (input === false) {
return 'Input is exactly false';
} else if (input === 0) {
return 'Input is zero';
} else if (typeof input === 'number') {
if (input > 0) {
return 'Input is a positive number';
} else if (input < 0) {
return 'Input is a negative number';
} else {
return 'Unknown input';
}
} else if (input === 'hello') {
return "Input is the string 'hello'";
} else if (typeof input === 'string') {
return 'Input is another string';
} else {
return 'Unknown input';
}
};
const analyzeInput2 = (input: string | number | boolean): string => {
// SESE
let sReturn;
switch (typeof input) {
case 'boolean':
sReturn = `Input is exactly ${input ? 'true' : 'false'}`;
break;
case 'number':
if (input === 0) {
sReturn = 'Input is zero';
} else {
sReturn = `Input is a ${input > 0 ? 'positive' : 'negative'} number`;
}
break;
case 'string':
if (input === 'hello') {
sReturn = "Input is the string 'hello'";
} else {
sReturn = 'Input is another string';
}
break;
default:
sReturn = 'Unknown input';
}
return sReturn;
};
console.log(analyzeInput(true)); // => "Input is exactly true"
console.log(analyzeInput(false)); // => "Input is exactly false"
console.log(analyzeInput(0)); // => "Input is zero"
@@ -10,8 +58,8 @@ console.log(analyzeInput(-7)); // => "Input is a negative number"
console.log(analyzeInput('hello')); // => "Input is the string 'hello'"
console.log(analyzeInput('world')); // => "Input is another string"
console.log(analyzeInput('')); // => "Input is another string"
console.log(analyzeInput(undefined)); // => "Unknown input"
console.log(analyzeInput(null)); // => "Unknown input"
// console.log(analyzeInput(undefined)); // => "Unknown input"
// console.log(analyzeInput(null)); // => "Unknown input"
// Übung 14: Equality Narrowing in einer Funktion implementieren
// Vervollständige die Funktion analyzeInput so, dass sie zurückgegeben wird:
@@ -1,13 +1,28 @@
type Guitar = { strum: () => void };
type Piano = { pressKeys: () => void };
const play = (instrument: Guitar | Piano) => {
// Your code here
const play = (instrument: Guitar | Piano): void => {
// Die Eigenschaft "strum" grenzt den Typ auf Guitar ein.
if ('strum' in instrument) {
instrument.strum();
} else {
instrument.pressKeys();
}
};
// Create guitar instance
const guitar: Guitar = {
strum: () => {
console.log('Strumming the guitar!');
},
};
// Create piano instance
const piano: Piano = {
pressKeys: () => {
console.log('Pressing piano keys!');
},
};
// Call functions
play(guitar); // => "Strumming the guitar!"
@@ -0,0 +1,60 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
{
const handleValue2 = (value) => {
if (typeof value === "string") {
console.log(value.toUpperCase());
} else if (typeof value === "number") {
console.log(value * 2);
} else if (typeof value === "boolean") {
console.log(value ? "It's true!" : "It's false!");
}
};
handleValue2("hello");
handleValue2("TypeScript");
handleValue2(21);
handleValue2(50);
handleValue2(false);
handleValue2(true);
}
{
let handleValue2 = function(value) {
let mReturn;
switch (typeof value) {
case "string":
mReturn = value.toUpperCase();
break;
case "number":
mReturn = value * 2;
break;
case "boolean":
mReturn = `It\xB4s ${String(value)}!`;
break;
default:
mReturn = "Something went wrong...";
}
};
handleValue = handleValue2;
handleValue2("hello");
handleValue2("TypeScript");
handleValue2(21);
handleValue2(50);
handleValue2(false);
handleValue2(true);
}
var handleValue;
}
});
require_main();
})();
@@ -1,10 +1,51 @@
// Test handleValue with different inputs
handleValue('hello'); // => "HELLO"
handleValue('TypeScript'); // => "TYPESCRIPT"
handleValue(21); // => 42
handleValue(50); // => 100
handleValue(false); // => "It's false!"
handleValue(true); // => "It's true!"
{
const handleValue = (value: string | number | boolean): void => {
if (typeof value === 'string') {
console.log(value.toUpperCase());
} else if (typeof value === 'number') {
console.log(value * 2);
} else if (typeof value === 'boolean') {
console.log(value ? "It's true!" : "It's false!");
}
};
handleValue('hello'); // => "HELLO"
handleValue('TypeScript'); // => "TYPESCRIPT"
handleValue(21); // => 42
handleValue(50); // => 100
handleValue(false); // => "It's false!"
handleValue(true); // => "It's true!"
}
{
function handleValue(value: string | number | boolean) {
let mReturn;
switch (typeof value) {
case 'string':
mReturn = value.toUpperCase();
break;
case 'number':
mReturn = value * 2;
break;
case 'boolean':
mReturn = `It's ${String(value)}!`;
break;
default:
// nothing
mReturn = 'Something went wrong...';
}
console.log(mReturn);
return mReturn;
}
handleValue('hello'); // => "HELLO"
handleValue('TypeScript'); // => "TYPESCRIPT"
handleValue(21); // => 42
handleValue(50); // => 100
handleValue(false); // => "It's false!"
handleValue(true); // => "It's true!"
}
// Übung 16: Type Narrowing mit Zuweisungsoperationen
@@ -14,9 +55,9 @@ handleValue(true); // => "It's true!"
// Anweisungen:
// 1Schreibe eine Funktion handleValue, die einen string | number | boolean Parameter annimmt und je nach Typ des Arguments ein anderes Verhalten protokolliert.
// 1. Schreibe eine Funktion handleValue, die einen string | number | boolean Parameter annimmt und je nach Typ des Arguments ein anderes Verhalten protokolliert.
// Wenn es eine string ist, protokolliere die Version in Großbuchstaben.
//- [ ] Wenn es eine string ist, protokolliere die Version in Großbuchstaben.
// Wenn es sich um eine number handelt, logge ihren Wert multipliziert mit 2.
@@ -0,0 +1,35 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
{
const handleInput = (value) => {
if (value === null) {
return "No value provided";
}
if (typeof value === "boolean") {
return value ? "True" : "False";
}
if (typeof value === "string") {
return value.toUpperCase();
}
return value * value;
};
console.log(handleInput(null));
console.log(handleInput(true));
console.log(handleInput("hello"));
console.log(handleInput(5));
}
}
});
require_main();
})();
@@ -1,8 +1,26 @@
// Test cases
console.log(handleInput(null)); // => "No value provided"
console.log(handleInput(true)); // => "True"
console.log(handleInput('hello')); // => "HELLO"
console.log(handleInput(5)); // => 25
{
const handleInput = (value: string | number | boolean | null): string | number => {
if (value === null) {
return 'No value provided';
}
if (typeof value === 'boolean') {
return value ? 'True' : 'False';
}
if (typeof value === 'string') {
return value.toUpperCase();
}
return value * value; // TypeScript knows this is a number
};
// Test cases
console.log(handleInput(null)); // => "No value provided"
console.log(handleInput(true)); // => "True"
console.log(handleInput('hello')); // => "HELLO"
console.log(handleInput(5)); // => 25
}
// Übung 17: Kontrollflussanalyse und Type Narrowing
@@ -0,0 +1,58 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
function isCar(vehicle) {
return typeof vehicle.drive !== "undefined";
}
function handleVehicle(vehicle) {
if (isCar(vehicle)) {
vehicle.drive();
} else {
vehicle.loadCargo();
}
}
var vehicles = [
{
drive() {
console.log("Car driving");
}
},
{
drive() {
console.log("Car driving");
}
},
{
loadCargo() {
console.log("Truck loading cargo");
}
},
{
loadCargo() {
console.log("Truck loading cargo");
}
},
{
drive() {
console.log("Car driving");
}
}
];
var cars = vehicles.filter(isCar);
cars.forEach((car) => car.drive());
handleVehicle({ drive: () => console.log("Car driving") });
handleVehicle({ loadCargo: () => console.log("Truck loading cargo") });
}
});
require_main();
})();
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
@@ -29,7 +29,7 @@
</li>
<li>
Erstelle eine Type Predicate Funktion isCar, um zu prüfen, ob ein bestimmtes Fahrzeug ein Car ist und eine
Fahrfunktion hat. Hinweis: .drive !== undefined.
Fahrfunktion hat. Hinweis: `typeof obj.drive !== 'undefined'`.
</li>
<li>
Implementiere eine Funktion handleVehicle, die einen Parameter vom Typ Car | Truck akzeptiert:
@@ -1,11 +1,3 @@
// Test the filtered array
const cars = vehicles.filter(isCar);
cars.forEach((car) => car.drive()); // => "Car driving"
// Test handleVehicle with both Car and Truck
handleVehicle({ drive: () => console.log('Car driving') }); // => "Car driving"
handleVehicle({ loadCargo: () => console.log('Truck loading cargo') }); // => "Truck loading cargo"
// Übung 18: Type Predicates implementieren
// In dieser Aufgabe definierst du zwei verschiedene Typen und implementierst eine benutzerdefinierte Predicate Function, um zwischen ihnen zu unterscheiden. Dann benutzt du diese Funktion, um verschiedene Objekte in einem Array zu filtern und zu behandeln.
@@ -14,17 +6,69 @@ handleVehicle({ loadCargo: () => console.log('Truck loading cargo') }); // => "T
// 1 Definiere zwei Typen, Car und Truck, wobei:
// Car hat eine Methode drive().
type Car = {
drive(): void;
// drive: () => void;
};
// Truck hat eine Methode loadCargo().
type Truck = {
loadCargo(): void;
};
// 2 Erstelle eine Type Predicate Funktion isCar, um zu prüfen, ob ein bestimmtes Fahrzeug ein Car ist und eine Fahrfunktion hat. Hinweis: .drive !== undefined.
function isCar(vehicle: Car | Truck): vehicle is Car {
// return Object.hasOwn(vehicle, 'drive');
return typeof (vehicle as Car).drive !== 'undefined';
}
// 3 Implementiere eine Funktion handleVehicle, die einen Parameter vom Typ Car | Truck akzeptiert:
// Wenn das Fahrzeug ein Car ist, rufe drive() an.
// Wenn das Fahrzeug ein Truck ist, rufe loadCargo() an.
function handleVehicle(vehicle: Car | Truck) {
if (isCar(vehicle)) {
vehicle.drive();
} else {
vehicle.loadCargo();
}
}
// 4 Erstelle ein Array von Car | Truck Objekten und filtere nur die Autos mit dem isCar Predicate heraus.
const vehicles = [
{
drive() {
console.log('Car driving');
},
},
{
drive() {
console.log('Car driving');
},
},
{
loadCargo() {
console.log('Truck loading cargo');
},
},
{
loadCargo() {
console.log('Truck loading cargo');
},
},
{
drive() {
console.log('Car driving');
},
},
];
// 5 Teste deine Funktionen mit verschiedenen Eingaben.
// Test the filtered array
const cars = vehicles.filter(isCar);
cars.forEach((car) => car.drive()); // => "Car driving"
// Test handleVehicle with both Car and Truck
handleVehicle({ drive: () => console.log('Car driving') }); // => "Car driving"
handleVehicle({ loadCargo: () => console.log('Truck loading cargo') }); // => "Truck loading cargo"
@@ -0,0 +1,28 @@
(() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
} catch (e) {
throw mod = 0, e;
}
};
// src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
var calculate = (operation, a, b) => {
return operation(a, b);
};
var add = (a, b) => {
return a + b;
};
var multiply = (a, b) => {
return a * b;
};
console.log(calculate(add, 10, 5));
console.log(calculate(multiply, 10, 5));
}
});
require_main();
})();
@@ -1,7 +1,3 @@
// Test cases
console.log(calculate(add, 10, 5)); // => 15
console.log(calculate(multiply, 10, 5)); // => 50
// Übung 19: Arbeiten mit Function Type Expressions
// In dieser Aufgabe schreibst du eine Funktion, die andere Funktionen als Parameter akzeptiert und Logik basierend auf diesen Function Type Expressions implementiert.
@@ -9,9 +5,25 @@ console.log(calculate(multiply, 10, 5)); // => 50
// Anweisungen:
// 1 Definiere einen Type Alias MathOperation für eine Funktion, die zwei numbers als Parameter nimmt und einen number zurückgibt.
type MathOperation = (a: number, b: number) => number;
// 2 Implementiere eine Funktion calculate, die eine Funktion vom Typ MathOperation und zwei numbers annimmt und die Funktion auf die Zahlen anwendet.
const calculate = (operation: MathOperation, a: number, b: number) => {
return operation(a, b);
};
// 3 Erstelle zwei neue Funktionen, add und multiply, die der Signatur MathOperation entsprechen.
const add: MathOperation = (a, b) => {
return a + b;
};
const multiply: MathOperation = (a, b) => {
return a * b;
};
// 4 Teste alle Funktionen, indem du geeignete Argumente übergibst und die Ergebnisse protokollierst.
// Test cases
console.log(calculate(add, 10, 5)); // => 15
console.log(calculate(multiply, 10, 5)); // => 50
@@ -0,0 +1,21 @@
(() => {
// src/main.ts
var double = (value) => value * 2;
var halve = (value) => value / 2;
var describeAndApply = (operation, value) => {
console.log(operation.description);
return operation(value);
};
var describableDouble = Object.assign(double, {
description: "This operation doubles the number."
});
var describableHalve = Object.assign(halve, {
description: "This operation halves the number."
});
console.log(describeAndApply(describableDouble, 10));
console.log(describeAndApply(describableHalve, 10));
console.log(describeAndApply(describableDouble, -3));
console.log(describeAndApply(describableHalve, 7));
console.log(double(0));
console.log(halve(0));
})();
@@ -9,5 +9,5 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
"type": "module"
}
@@ -1,23 +1,3 @@
// Step 1: Define the NumberOperation call signature
// Step 2: Implement double and halve functions
// Step 3: Define the DescribableNumberOperation type
// Step 4: Implement describeAndApply function
// Test all functions
const describableDouble: DescribableNumberOperation = Object.assign(double, {
description: 'This operation doubles the number.',
});
const describableHalve: DescribableNumberOperation = Object.assign(halve, {
description: 'This operation halves the number.',
});
console.log(describeAndApply(describableDouble, 10)); // => 20
console.log(describeAndApply(describableHalve, 10)); // => 5
// Übung 20: Call Signatures für Zahlenoperationen
// In dieser Übung erstellst und verwendest du ein paar einfache Funktionen, die sich an bestimmte Call Signatures für die Arbeit mit Zahlen halten.
@@ -26,13 +6,54 @@ console.log(describeAndApply(describableHalve, 10)); // => 5
// 1 Definiere eine Funktion vom Typ NumberOperation, die ein einzelnes number annimmt und ein number zurückgibt.
// Die Call Signature beschreibt, wie eine passende Funktion aufgerufen wird.
type NumberOperation = {
(value: number): number;
};
type NumberFn = (value: number) => number;
// 2 Implementiere zwei Funktionen, double und halve, die der Call Signature NumberOperation folgen:
// double: Multipliziert die eingegebene Zahl mit 2.
// halve: Teilt die eingegebene Zahl durch 2.
// Der Parametertyp wird jeweils aus NumberOperation abgeleitet.
const double: NumberOperation = (value) => value * 2;
const halve: NumberOperation = (value) => value / 2;
const double2: NumberFn = (value) => value * 2;
const halve2: NumberFn = (value) => value / 2;
// 3 Definiere einen Objekttyp DescribableNumberOperation mit einer description-Property vom Typ string und einer Cal Signature, die NumberOperation entspricht.
// Dieser Typ kombiniert eine Eigenschaft mit einer Call Signature.
type DescribableNumberOperation = {
description: string;
(value: number): number;
};
// 4 Implementiere eine Funktion describeAndApply, die ein DescribableNumberOperation Objekt annimmt, dessen description protokolliert und die Operation auf eine bestimmte Zahl anwendet.
const describeAndApply = (operation: DescribableNumberOperation, value: number): number => {
console.log(operation.description);
return operation(value);
};
// 5 Teste alle Funktionen mit ein paar Zahlen.
// Object.assign ergänzt die Funktionen um die description-Eigenschaft.
// Die Funktionen bleiben dabei aufrufbar.
const describableDouble: DescribableNumberOperation = Object.assign(double, {
description: 'This operation doubles the number.',
});
const describableHalve: DescribableNumberOperation = Object.assign(halve, {
description: 'This operation halves the number.',
});
console.log(describeAndApply(describableDouble, 10)); // Beschreibung, dann 20
console.log(describeAndApply(describableHalve, 10)); // Beschreibung, dann 5
console.log(describeAndApply(describableDouble, -3)); // Beschreibung, dann -6
console.log(describeAndApply(describableHalve, 7)); // Beschreibung, dann 3.5
console.log(double(0)); // => 0
console.log(halve(0)); // => 0
@@ -0,0 +1,10 @@
(() => {
// src/main.ts
var identity = (arg) => {
return arg;
};
console.log(identity(42));
console.log(identity("TS sucks"));
console.log(identity(["hello", "world"]));
console.log(identity(["hello", "world"]));
})();
@@ -9,5 +9,5 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
"type": "module"
}
@@ -5,11 +5,18 @@
// Anweisungen:
// 1 Implementiere eine generische identity Funktion, die einen Parameter vom Typ T annimmt und denselben Wert zurückgibt.
const identity = <T>(arg: T): T => {
return arg;
};
// 2 Rufe identity mit einer number auf und speichere das Ergebnis.
console.log(identity<number>(42));
// 3 Rufe identity mit einer string auf und speichere das Ergebnis.
console.log(identity<string>('TS sucks'));
// 4 Rufe identity mit einem Array von Objekten auf und speichere das Ergebnis.
console.log(identity<string[]>(['hello', 'world']));
// 5 Stelle sicher, dass TypeScript die Rückgabetypen ohne explicit Type Annotations korrekt herleitet.
console.log(identity(['hello', 'world']));
@@ -17,6 +17,9 @@
if (typeof value === "string") {
return `String: ${value}`;
}
if (Array.isArray(value)) {
return `String: ${value.join(", ")}`;
}
return `Number: ${value.toFixed(2)}`;
};
console.log(checkValue("hello"));
@@ -4,6 +4,7 @@
const isFish = (pet2) => {
return Object.hasOwn(pet2, "swim");
};
const isBird = (pet2) => "fly" in pet2;
const handlePet = (pet2) => {
if (isFish(pet2)) {
pet2.swim();
@@ -23,6 +24,8 @@
{ swim: () => console.log("Another Fish flying") }
];
const fishes = zoo.filter((pet2) => isFish(pet2));
const birds = zoo.filter(isBird);
fishes.forEach((fish) => fish.swim());
birds.forEach((bird) => bird.fly());
}
})();
@@ -14,7 +14,7 @@
// vehicle.drive(); // Property 'drive' does not exist on type 'Car | Boat'.
// Object.hasOwn() führt kein narrowing aus | grenzt nicht den Datentyp ein
// if (Object.hasOwn(vehicle, 'drive')) {
//if (Object.hasOwn(vehicle, 'drive')) {
if ('drive' in vehicle) {
vehicle.drive();
} else {
@@ -21,11 +21,14 @@ Type Narrowing wird verwendet, um den Typ einer Variablen zu beschränken, wenn
// Kontrollfluss mit Early Returns
const checkValue = (value: string | number): string => {
const checkValue = (value: string | number | number[]): string => {
// Guard
if (typeof value === 'string') {
return `String: ${value}`; // early return bei Narrowing
}
if (Array.isArray(value)) {
return `String: ${value.join(', ')}`;
}
return `Number: ${value.toFixed(2)}`;
};
@@ -17,6 +17,7 @@ WICHTIG: "Außerdem grenzt es den Typ der Variablen auf der Grundlage des Ergebn
// return 'swim' in pet; // in Operator führt Type-Narrowing durch
};
const isBird = (pet: Fish | Bird): pet is Bird => 'fly' in pet;
const handlePet = (pet: Fish | Bird) => {
// Property 'swim' does not exist on type 'Fish | Bird'.
@@ -52,6 +53,8 @@ WICHTIG: "Außerdem grenzt es den Typ der Variablen auf der Grundlage des Ergebn
];
const fishes = zoo.filter((pet) => isFish(pet));
const birds = zoo.filter(isBird);
fishes.forEach((fish) => fish.swim());
birds.forEach((bird) => bird.fly());
}