This commit is contained in:
@@ -1,2 +1,75 @@
|
||||
(() => {
|
||||
// src/04_never-void-unknown.ts
|
||||
{
|
||||
let infiniteLoop = function() {
|
||||
while (true) {
|
||||
console.log("This loop will never end");
|
||||
}
|
||||
};
|
||||
const logMessage = (message) => {
|
||||
console.log(message);
|
||||
};
|
||||
const logMessage2 = (message) => {
|
||||
console.log(message);
|
||||
};
|
||||
logMessage("Hello, TypeScript!");
|
||||
console.log(logMessage2("Hello, TypeScript!"));
|
||||
const clearScreen = () => {
|
||||
};
|
||||
clearScreen();
|
||||
const processInput = (input) => {
|
||||
if (typeof input === "string") {
|
||||
console.log(`The input is a string: ${input.toUpperCase()}`);
|
||||
} else if (typeof input === "number") {
|
||||
console.log(`The input is a number: ${input + 10}`);
|
||||
} else {
|
||||
console.log("Unknown type of input");
|
||||
}
|
||||
};
|
||||
processInput("Hello");
|
||||
processInput(42);
|
||||
processInput(true);
|
||||
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.log(err.message);
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
throw new Error("An unknown error occurred.");
|
||||
}
|
||||
};
|
||||
getPersonAsync().then((data) => console.log(data));
|
||||
const safeParse = (s) => {
|
||||
return JSON.parse(s);
|
||||
};
|
||||
const result = safeParse('{"name": "Alice"}');
|
||||
if (typeof result === "object" && result !== null && "name" in result) {
|
||||
console.log(result.name);
|
||||
}
|
||||
const throwError = (message) => {
|
||||
throw new Error(message);
|
||||
};
|
||||
throwError("Something went wrong!");
|
||||
const handleShape = (shape) => {
|
||||
switch (shape) {
|
||||
case "circle":
|
||||
return "Handling a circle";
|
||||
case "square":
|
||||
return "Handling a square";
|
||||
default:
|
||||
const _exhaustiveCheck = shape;
|
||||
return _exhaustiveCheck;
|
||||
}
|
||||
};
|
||||
console.log(handleShape("circle"));
|
||||
console.log(handleShape("square"));
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user