This commit is contained in:
Philippe Torrel
2026-07-28 11:33:14 +02:00
parent a3f7493bd0
commit 0cecd61f11
22 changed files with 872 additions and 61 deletions

View File

@@ -1,18 +1,10 @@
(() => { (() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// src/main.ts // src/main.ts
var require_main = __commonJS({ {
"src/main.ts"() { const calculateDiscount = (price, discount) => {
function calculateDiscount(price, discount) { return price - discount;
return Number((price - discount).toFixed(2)); };
const finalPrice = calculateDiscount(100, 10);
console.log(finalPrice);
} }
var finalPrice = calculateDiscount(100, 10);
console.log(`The final price is $${finalPrice}`);
}
});
require_main();
})(); })();

View File

@@ -9,5 +9,5 @@
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "commonjs" "type": "module"
} }

View File

@@ -1,5 +1,9 @@
function calculateDiscount(price: number, discount: string): number { {
return price - parseFloat(discount); const calculateDiscount = (price: number, discount: number): number => {
} return price - discount;
};
const finalPrice = calculateDiscount(100, 10); const finalPrice = calculateDiscount(100, 10);
console.log(finalPrice);
}

View File

@@ -1,18 +1,16 @@
(() => { (() => {
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// src/main.ts // src/main.ts
var require_main = __commonJS({
"src/main.ts"() {
var user = { var user = {
name: "John Doe", name: "John Doe",
age: 30 age: 30
}; };
console.log(user.email?.toLowerCase()); user.email = "johndoe@mail.com";
} var user2 = {
}); name: "Jane Doe",
require_main(); age: 30
};
console.log(user.email ? user.email : "no email");
console.log(user2.email ? user2.email : "no email");
console.log("Email user: ", user?.email);
console.log("Email user2: ", user2?.email);
})(); })();

View File

@@ -9,5 +9,5 @@
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "commonjs" "type": "module"
} }

View File

@@ -1,6 +1,22 @@
const user = { interface User {
name: string;
age: number;
email?: string;
}
const user: User = {
name: 'John Doe', name: 'John Doe',
age: 30, age: 30,
}; };
console.log(user.email.toLowerCase()); user.email = 'johndoe@mail.com';
const user2: User = {
name: 'Jane Doe',
age: 30,
};
console.log(user.email ? user.email : 'no email');
console.log(user2.email ? user2.email : 'no email');
console.log('Email user: ', user?.email);
console.log('Email user2: ', user2?.email);

View File

@@ -0,0 +1,22 @@
(() => {
// src/main.ts
{
let operateVehicle = function(vehicle) {
if (vehicle.startEngine("car-key")) {
return vehicle.drive(60);
} else {
return "Engine failed to start.";
}
};
const myCar = {
startEngine: (key) => {
console.log(`Engine started with key: ${key}`);
return true;
},
drive: (speed) => {
return `Driving at ${speed} km/h`;
}
};
console.log(operateVehicle(myCar));
}
})();

View File

@@ -9,5 +9,5 @@
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "commonjs" "type": "module"
} }

View File

@@ -1,24 +1,26 @@
interface Vehicle { {
interface Vehicle {
startEngine: (key: string) => boolean; startEngine: (key: string) => boolean;
drive: (speed: number) => string; drive: (speed: number) => string;
} }
const myCar = { const myCar = {
startEngine: () => { startEngine: (key: string): boolean => {
console.log('Engine started'); console.log(`Engine started with key: ${key}`);
return true; return true;
}, },
drive: () => { drive: (speed: number): string => {
return 'Driving at default speed'; return `Driving at ${speed} km/h`;
}, },
}; };
function operateVehicle(vehicle: Vehicle): string { function operateVehicle(vehicle: Vehicle): string {
if (vehicle.startEngine('car-key')) { if (vehicle.startEngine('car-key')) {
return vehicle.drive(60); return vehicle.drive(60);
} else { } else {
return 'Engine failed to start.'; return 'Engine failed to start.';
} }
} }
console.log(operateVehicle(myCar)); console.log(operateVehicle(myCar));
}

View File

@@ -2,6 +2,7 @@
// src/09_type-inference.ts // src/09_type-inference.ts
{ {
let greeting = "Hello World"; let greeting = "Hello World";
greeting = "Hello TS!";
let count = 123; let count = 123;
let isCompleted = true; let isCompleted = true;
const isSmoking = true; const isSmoking = true;

View File

@@ -14,3 +14,9 @@
return cart.getTotal(); return cart.getTotal();
}; };
} }
// Das Prinzip: void bedeutet „Ignoriere die Rückgabe“, nicht „Es darf nichts zurückgegeben werden“
// Wenn du ein Interface oder einen Funktionstyp mit void definierst, sagst du TypeScript damit: „Es ist mir völlig egal, was diese Funktion zurückgibt. Ich werde mit dem Rückgabewert sowieso nichts tun.“
// Es ist also ein Mindestversprechen. Die Funktion erfüllt den Vertrag, indem sie aufgerufen werden kann. Wenn sie zusätzlich noch eine Zahl (oder einen String) zurückgibt, ignoriert TypeScript das einfach an der Stelle, wo das Interface genutzt wird.

View File

@@ -5,6 +5,8 @@
// greeting = 123; // Type 'number' is not assignable to type 'string'. // greeting = 123; // Type 'number' is not assignable to type 'string'.
greeting = 'Hello TS!';
let count = 123; // inferred as number let count = 123; // inferred as number
let isCompleted = true; //inferred as boolean let isCompleted = true; //inferred as boolean

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

View File

@@ -0,0 +1,44 @@
(() => {
// src/01_grundtypen.ts
{
let greeting = "Welcome to TypeScript";
const greeting2 = "Welcome to TypeScript";
let wholeNumber = 10;
let decimalNumber = 19.99;
let isLearning = true;
let age = 41;
let age2 = 30;
let scores = [10, 20, 30, 40];
let moreScores = [50, 60, 70];
const mixedScores = [10, 20, 30, "40"];
const moreMixedScores = [50, "60", 70];
let randomValue = 42;
randomValue = "Now I'm a string!";
randomValue = { name: "TypeScript" };
const $ = (qs) => document.querySelector(qs);
const $$ = (qs) => Array.from(document.querySelectorAll(qs));
const multiply = (a, b) => {
return a * b;
};
console.log(multiply(2, 4));
let bigNumber = 9007199254740992n;
console.log(Number.MAX_SAFE_INTEGER);
const id = /* @__PURE__ */ Symbol(1);
const id2 = /* @__PURE__ */ Symbol(1);
console.log(id === id2);
console.log(
greeting,
greeting2,
wholeNumber,
decimalNumber,
isLearning,
age,
age2,
scores.join(", "),
moreScores.join(", "),
mixedScores.join(", "),
moreMixedScores.join(", "),
bigNumber
);
}
})();

View File

@@ -0,0 +1,43 @@
(() => {
// src/02_fetch-example.ts
{
const fetchData = async () => {
return "Data fetched successfully!";
};
const getPerson = () => {
return new Promise((resolve, reject) => {
fetch("https://dummyjson.com/users/1?select=firstName,lastName,age").then((res) => {
if (!res.ok) throw Error("HTTP-Request went wrong.");
return res.json();
}).then((data) => {
resolve(data);
}).catch((err) => {
console.error(err);
reject(err);
});
});
};
const getPersonAsync = async () => {
try {
const res = await fetch("https://dummyjson.com/users/1?select=firstName,lastName,age");
if (!res.ok) {
throw Error(`HTTP-Request went wrong: ${res.status}`);
}
const data = await res.json();
return data;
} catch (err) {
if (err instanceof Error) {
console.error(err);
throw err;
}
throw new Error("An unknown error occurred.");
}
};
getPerson().then((data) => {
console.log(data);
});
getPersonAsync().then((data) => {
console.log(data);
});
}
})();

View File

@@ -0,0 +1,2 @@
(() => {
})();

View File

@@ -0,0 +1,24 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TypeScript - Types</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- <script src="assets/js/01_grundtypen.js" defer></script> -->
<script src="assets/js/02_fetch-example.js" defer></script>
</head>
<body>
<main>
<div class="container py-5">
<h1>TypeScript - Types</h1>
<p>
TypeScript ist eine von Microsoft entwickelte Skriptsprache, die auf den Vorschlägen zum
ECMAScript-6-Standardbasiert und statische Typisierung zu JavaScript hinzufügt. Sprachkonstrukte von
TypeScript, wie Klassen, Vererbung, Module und anonyme Funktionen, wurden auch in ECMAScript 6 übernommen.
</p>
<img src="assets/img/Understand-Typescript.jpg" alt="TypeScript Kreisdiagramm" class="img-thumbnail" />
</div>
</main>
</body>
</html>

View File

@@ -0,0 +1,499 @@
{
"name": "01_ts-types",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "01_ts-types",
"version": "1.0.0",
"devDependencies": {
"esbuild": "^0.28.1"
}
},
"node_modules/@esbuild/aix-ppc64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.1.tgz",
"integrity": "sha512-Svl7tq8k/08+p6CXPpRjQ1fKX+1odH/BQbb48fV6fj3CWHhsoIOoY87w1oHXm0qEpkIK3ZfVgp0hed3XBXzXMQ==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"aix"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-arm": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.1.tgz",
"integrity": "sha512-0k2F129Xdio1TdJfzJ8sy1Q47vUD2NnwdhiAf7drUN1EBTfPf4hsFCtmMgu/6m8JSzsBrlmVjudMBQqOfG8usQ==",
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.1.tgz",
"integrity": "sha512-34EGEbCIAgosYz6goLcopX6Mo7NyGv9tfwEM2/7Ce2VcVRk568iSvniGWcUXIy7wEDR1wzolcxcriFVrWYcwBg==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.1.tgz",
"integrity": "sha512-dbwY7ltSMDWsRatcRpCnES4F+im88OCUgGZjy52shC7GqHRE/cYlxNbB4Z4UpJswpcc4Qxd2oE/ufM0p61IKng==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/darwin-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.1.tgz",
"integrity": "sha512-TZbWkQY7kvTAXbXUT7uVACR5cMHsDiSz9z7ZKAX/RTq/WJEk3QyRr0wZpNhBDX+/0CtdqUIJlOiodQcta6tY3Q==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/darwin-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.1.tgz",
"integrity": "sha512-zfdzgK9ACBNZLI/CyHTOx81SyNbM6YXn7rxSgX97VjyiPl9W1i4Ka4fgKECEoFCKGpvBj5qArWIGgQjOwkgskQ==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/freebsd-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.1.tgz",
"integrity": "sha512-wG2EA8ENdEI0qhkSZMjfqrdY+ziCYCPMmtZjjIwOmXFjmyzEHn+UUxk5of+SYsjtfs3VpnlC7QLzSI5hY/rOAw==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/freebsd-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.1.tgz",
"integrity": "sha512-i7dZ9vQgnvSCzi/rYCXNgtF/U+eKZNJBzu3eTQbRgHnM7tNSizLOkRFAl3qzVc/Op/u5YkHHa4pf/3DOYHthLQ==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-arm": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.1.tgz",
"integrity": "sha512-qVXBOHQS+d5Y722GwJzJUtOLlX7km3CraOaGormF1pDtPd2C/l1SHRPgjLunLGe51Sh5YYWKMFDyV4SxgMQYTQ==",
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.1.tgz",
"integrity": "sha512-yHs+0uc8+nvEAfAfxrWQKK5peSNzBc4PegcMO0EJ2hT71uA7vB8Ihg2e77R2P7SG5uYjPbHlLLmve4LLLRCf0g==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-ia32": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.1.tgz",
"integrity": "sha512-d1z4ZuP0ajrfz/FhGT4vv278rX8KnPPJx8i5+AtK7TYbx9Le9F1hyzurZpkEyjkGa9dUGhQow4C1NmeGvqxN2w==",
"cpu": [
"ia32"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-loong64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.1.tgz",
"integrity": "sha512-M5sRjUVZrkm1OAPR3dlOYzNmN+loZKGVi1VUQGrwuqLcbR6qeAz+famMhjASeH3YVKvZz+zT1jlh/keC3Rj/lg==",
"cpu": [
"loong64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-mips64el": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.1.tgz",
"integrity": "sha512-mRObBZeHh2OxcBFPWE/FjylkRgZdYuiTR3vaTozquCGOH14iP9oN4x4Ge81CoIDYQrXmIxpFumJBu5MtZpnQJQ==",
"cpu": [
"mips64el"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-ppc64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.1.tgz",
"integrity": "sha512-slScBsMAb3GFDcdrCgLwZtPYRoH2H/youv10QiZyRjmsP48fznoveWytSgCI/R0ZcUgpc0ZhIUEx6LHts8yrfQ==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-riscv64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.1.tgz",
"integrity": "sha512-kw0owk1o0GFETUJyW0jc0G4Yzs0BHZn0JDZ8JRT088vjJYX777BAs1fDGxAC+q831qOs2DTC96mNsG2opdfyyQ==",
"cpu": [
"riscv64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-s390x": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.1.tgz",
"integrity": "sha512-/lAIjX8aYFRByhh6L5rYtPEDRqa9de/4V/juOXcta5frjvzXO4/sqEtyytse0g3zZFuWu5cDN0MkLz2qRDD2Ag==",
"cpu": [
"s390x"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.1.tgz",
"integrity": "sha512-u/anNYF2mmVOEDwLtnQ1wOr3EZ9sTNGLWrsYGYwHWzGA3Si84IOkHXlbWTD1NB+9/1lcnweYKO54uhxZydNzfA==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/netbsd-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.1.tgz",
"integrity": "sha512-oks0DYbLwWMmaakTsCb+zL4E+aHRVLom9IJZOAthMQEPiQmydXHkziYEsGYRx0uNV/IjEKGAV941JzH02pflqw==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/netbsd-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.1.tgz",
"integrity": "sha512-aeL6lAnN89Hz43Mlh1G8ARasbuoYvSITDEx0tHh5b7jJnHcssqgjy9Yx430GDpmCa6OyrKoS0aNRjKundRizGg==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openbsd-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.1.tgz",
"integrity": "sha512-MEFJe5C3R8pwXdZ5Y21oo6m7ePiS0d9pWucn99O/wvyJZChoIQKrQDxKrGeW8F5+T0okTHesAmDeiHDTIq0V/Q==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openbsd-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.1.tgz",
"integrity": "sha512-i/ZLIOafE0Z8cI/XANJAixoJL/uRAoS2xOA3rb0xN+KK0K177cMAsQYkzHtBrtMXAKuAc7HGgcWiZ/sRC1Nxgw==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openharmony-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.1.tgz",
"integrity": "sha512-ge+Z7EXFNt2BO1oAMsVpiQ8EwndV9i1xXerAeTIK7AtPs3bKFXQM7nlRxDSIUIMeueR1CNXxqztLzdNeReKBJg==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openharmony"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/sunos-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.1.tgz",
"integrity": "sha512-BEjgtECkL3vY+SaSQ6nzVfiALUeFxpawyp8Jmf5PtYhf1Ug40N1h/hxlhts+f1FvSvarEigdxS3BlSMI2PJLcQ==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"sunos"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-arm64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.1.tgz",
"integrity": "sha512-lCv9eK/H6ZJWbE7bh2nw54CZ9M2nupBxJcTsdk/QQnWkdSjKGuxmmH8/GWrlT1eMmZfn4dGcCjRte397WqfQXA==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-ia32": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.1.tgz",
"integrity": "sha512-zvb/mB2bSCoJOpoCBgYKKpX6YM6mJBlBUVUtVj41DlZJVEB6/0CKlRYxP5wWl1C1ILiCoAU5wZZ4q1P3qeS6Eg==",
"cpu": [
"ia32"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-x64": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.1.tgz",
"integrity": "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/esbuild": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.1.tgz",
"integrity": "sha512-HrJrvZv5ayxBzPfwphOoNzkzOIIlifzk0KJrGK2c8R4+LKpMtpYLQeUdjnwjWv/LZlkH2laZk+4w78pi99D4Vw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"bin": {
"esbuild": "bin/esbuild"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"@esbuild/aix-ppc64": "0.28.1",
"@esbuild/android-arm": "0.28.1",
"@esbuild/android-arm64": "0.28.1",
"@esbuild/android-x64": "0.28.1",
"@esbuild/darwin-arm64": "0.28.1",
"@esbuild/darwin-x64": "0.28.1",
"@esbuild/freebsd-arm64": "0.28.1",
"@esbuild/freebsd-x64": "0.28.1",
"@esbuild/linux-arm": "0.28.1",
"@esbuild/linux-arm64": "0.28.1",
"@esbuild/linux-ia32": "0.28.1",
"@esbuild/linux-loong64": "0.28.1",
"@esbuild/linux-mips64el": "0.28.1",
"@esbuild/linux-ppc64": "0.28.1",
"@esbuild/linux-riscv64": "0.28.1",
"@esbuild/linux-s390x": "0.28.1",
"@esbuild/linux-x64": "0.28.1",
"@esbuild/netbsd-arm64": "0.28.1",
"@esbuild/netbsd-x64": "0.28.1",
"@esbuild/openbsd-arm64": "0.28.1",
"@esbuild/openbsd-x64": "0.28.1",
"@esbuild/openharmony-arm64": "0.28.1",
"@esbuild/sunos-x64": "0.28.1",
"@esbuild/win32-arm64": "0.28.1",
"@esbuild/win32-ia32": "0.28.1",
"@esbuild/win32-x64": "0.28.1"
}
}
}
}

View File

@@ -0,0 +1,14 @@
{
"name": "01_ts-types",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"ts": "npx esbuild \"src/*.ts\" --watch --bundle --outdir=\"assets/js\" --loader:.ts=ts"
},
"keywords": [],
"type": "module",
"devDependencies": {
"esbuild": "^0.28.1"
}
}

View File

@@ -0,0 +1,83 @@
// Primitive Types - string, number, boolean, null, undefined, (Symbol) und bigint
{
let greeting: string = 'Welcome to TypeScript';
// prefer-coonst
const greeting2: string = 'Welcome to TypeScript';
let wholeNumber: number = 10;
let decimalNumber: number = 19.99;
let isLearning: boolean = true;
let age: number = 41; // RECOMMENDED explicit type annotation
let age2 = 30; // type inference - 'age' is inferred to be a number
// Array
let scores: number[] = [10, 20, 30, 40];
let moreScores: Array<number> = [50, 60, 70];
// Unions mit Array
const mixedScores: (number | string)[] = [10, 20, 30, '40'];
const moreMixedScores: Array<number | string> = [50, '60', 70];
// any - Es kann jeden Datentyp enthalten
// RECOMMENDED - versuchen "any" zu vermeiden
let randomValue: any = 42;
randomValue = "Now I'm a string!";
randomValue = { name: 'TypeScript' };
// randomValue.doSomething(); // No error in ts, even though `doSomething` doesn't exist.
// randomValue(); // Uncaught TypeError: randomValue.doSomething is not a function
// const $on = (elOrAr: Node | Node[], type: string, fn: EventListener) => {
// if (Array.isArray(elOrAr)) {
// elOrAr.forEach((ae) => $on(ae, type, fn));
// } else {
// elOrAr.addEventListener(type, fn);
// }
// return elOrAr;
// };
const $ = (qs: string) => document.querySelector(qs) as Node;
const $$ = (qs: string): Node[] => Array.from(document.querySelectorAll(qs));
// $on($('h1'), 'click', (e) => {
// console.log('click');
// });
// $on($$('ul li'), 'click', (e) => {
// console.log('click');
// });
// Funktionen
const multiply = (a: number, b: number): number => {
return a * b;
};
// console.log(multiply($('input').value, 4));
// console.log(multiply('2', 4)); // Argument of type 'string' is not assignable to parameter of type 'number'.
console.log(multiply(2, 4)); // => 8
let bigNumber: bigint = 9007199254740992n;
console.log(Number.MAX_SAFE_INTEGER);
// Symbol
const id: symbol = Symbol(1);
const id2: symbol = Symbol(1);
console.log(id === id2); // => false
console.log(
greeting,
greeting2,
wholeNumber,
decimalNumber,
isLearning,
age,
age2,
scores.join(', '),
moreScores.join(', '),
mixedScores.join(', '),
moreMixedScores.join(', '),
bigNumber,
);
}

View File

@@ -0,0 +1,59 @@
// Promise
{
// async function fetchData(): Promise<string> {
// return 'Data fetched successfully!';
// }
const fetchData = async (): Promise<string> => {
return 'Data fetched successfully!';
};
interface Userdata {
id?: number;
firstName: string;
lastName: string;
age: number;
}
const getPerson = (): Promise<Userdata> => {
return new Promise((resolve, reject) => {
fetch('https://dummyjson.com/users/1?select=firstName,lastName,age')
.then((res: Response) => {
if (!res.ok) throw Error('HTTP-Request went wrong.');
return res.json(); // <- Promise HTTP-Response Body - JS Objekt geparsed
})
.then((data: Userdata) => {
// console.log(data);
resolve(data);
})
.catch((err) => {
console.error(err);
reject(err);
});
});
};
const getPersonAsync = async (): Promise<Userdata> => {
try {
const res: Response = await fetch('https://dummyjson.com/users/1?select=firstName,lastName,age');
if (!res.ok) {
throw Error(`HTTP-Request went wrong: ${res.status}`);
}
const data: Userdata = await res.json();
return data;
} catch (err: unknown) {
if (err instanceof Error) {
console.error(err);
throw err; // paranoid
}
throw new Error('An unknown error occurred.');
}
};
getPerson().then((data) => {
console.log(data);
});
getPersonAsync().then((data) => {
console.log(data);
});
}