Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 7: type script #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"scripts": {
"lint": "eslint src/",
"build": "npm run lint"
"build": "tsc"
},
"devDependencies": {
"@eslint/js": "^9.19.0",
Expand Down
11 changes: 11 additions & 0 deletions src/lesson7/arrow-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const sumArrayArrow = (arr: (number | string)[]): number => {
return arr.reduce((acc, item) => acc + Number(item), 0);
};

// Приклади масивів
const numberArray2: number[] = [1, 2, 3, 4];
const stringArray2: string[] = ["10", "20", "30"];
Comment on lines +6 to +7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

явна vs неявна типізація. В даному випадку при ініціалізації змінної ТС знає кий тип буде у змінній


// Виклики стрілочної функції та виведення результатів
console.log("Sum of numberArray2 =", sumArrayArrow(numberArray2)); // 10
console.log("Sum of stringArray2 =", sumArrayArrow(stringArray2)); // 60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

функція написана так, що є баг, та з назви функція я очікую, що вона складатиме стрінги як стрінги, а не каститиме це у намбери

const stringArray2 = ["10", "20", "30", 'type'];
console.log("Sum of stringArray2 =", sumArrayArrow(stringArray2));  // NaN

16 changes: 16 additions & 0 deletions src/lesson7/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function sumArray(arr: (number | string)[]): number {
let total = 0;
for (const item of arr) {
// Перетворюємо кожен елемент у число
total += typeof item === "number" ? item : Number(item);
}
return total;
}

// Приклади масивів
const numberArray: number[] = [10, 20, 30];
const stringArray: string[] = ["5", "15", "25"];

// Виклики функції та виведення результатів у консоль
console.log("Sum of numberArray =", sumArray(numberArray)); // 60
console.log("Sum of stringArray =", sumArray(stringArray)); // 45
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "dist"
"skipLibCheck": true
},
"include": ["src"]
}
}