This commit is contained in:
miqlangelo 2025-05-05 15:33:51 +02:00
parent 0fba708d7a
commit b96d698d7f
5 changed files with 119 additions and 1 deletions

View File

@ -14,7 +14,7 @@ import {
sortNumbersDescending,
squareNumbers,
sumNumbers,
} from './starter.ts';
} from './solution.ts';
Deno.test('squareNumbers quadriert alle Zahlen', () => {
assertEquals(squareNumbers([1, 2, 3]), [1, 4, 9]);

View File

@ -0,0 +1,31 @@
type Circle = { kind: 'circle'; radius: number };
type Square = { kind: 'square'; sideLength: number };
type Triangle = { kind: 'triangle'; base: number; height: number };
type Shape = Circle | Square | Triangle;
function calculateArea(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.sideLength ** 2;
case 'triangle':
return (shape.base * shape.height) / 2;
default: {
const _exhaustiveCheck: never = shape;
throw new Error(
`Unhandled discrimated union case: ${JSON.stringify(_exhaustiveCheck)}`
);
}
}
}
const circle: Circle = { kind: 'circle', radius: 5 };
const square: Square = { kind: 'square', sideLength: 10 };
const triangle: Triangle = { kind: 'triangle', base: 5, height: 10 };
console.log(calculateArea(circle));
console.log(calculateArea(square));
console.log(calculateArea(triangle));

View File

@ -0,0 +1,38 @@
let person: [string, number];
person = ['Alice', 30];
// person = [30, "Alice"];
type Coordinate = [number, number];
const point: Coordinate = [10, 20];
function getX(coordinate: Coordinate): number {
return coordinate[0];
}
console.log(getX(point));
type Person = [string, number, string?];
const user1: Person = ['Alice', 30];
const user2: Person = ['Bob', 42, 'Developer'];
const [name, age, role] = user2;
console.log(name, age, role);
const user = {
name: 'Charly',
age: 23,
role: 'Admin',
};
const { name: tmpName, role: tmpRole } = user;
console.log('Name: ', tmpName);
console.log('Role: ', tmpRole);
// const [isActive, setIsActive] = useState(false);
// function useState(value: any) {
// return [value, (newvalue: any) => void ];
// }
type LogEntry = [string, ...number[]];
const log: LogEntry = ['Fehlermeldung', 404, 500, 501];

View File

@ -0,0 +1,49 @@
type StringOrNumber = string | number;
function combine(input: StringOrNumber, extra: StringOrNumber): StringOrNumber {
if (typeof input === 'number' && typeof extra === 'number') {
return input + extra;
}
return input.toString() + extra.toString();
}
console.log(combine(1, 'test'));
type Person = { name: string };
type Worker = { company: string };
type EmployeedPerson = Person & Worker;
const employee: EmployeedPerson = {
name: 'Alice',
company: 'TechCorp',
};
console.log(employee);
// const value: StringOrNumber = true
function formatInput(input: StringOrNumber): string {
return `Eingabe: ${input}`;
}
console.log(formatInput('Test'));
console.log(formatInput(123));
function getLength(value: string | number): number {
if (typeof value === 'string') {
return value.length;
}
return value.toString().length;
}
type A = { id: string };
type B = { id: number };
type C = A & B;
// const c: C = {
// // id: "test"
// // id: 4,
// };