What is TypeScript? 💡
TypeScript (TS) is a programming language created by Microsoft that extends JavaScript by adding strict typing. This makes the code more predictable and reduces the number of errors. 🚀
TypeScript is compiled into regular JavaScript, which allows it to be used in any project where JS works.
Why study TypeScript? 🎯
Safety: typing prevents errors.
Readability: the code becomes clearer.
ES6+ support: new JavaScript functions work without problems.
Compatibility: can be used in any JS project.
How to install TypeScript? 💻
To work with TypeScript, you will need:
✅ Node.js (to run the code). You can download it from official website.
✅ TypeScript Compiler. You can install it via the terminal:
npm install -g typescriptHow to check if TypeScript is installed? 🛠
Open the terminal and enter:
tsc --versionIf TypeScript is installed, its version will appear. Now you can write code! 🚀
TypeScript Syntax Basics 📚
1. Variables 📦
In TypeScript, variables are declared with types.
Example of declaring variables:
let name: string = "Alice"; // Line
const age: number = 25; // Number
let pi: number = 3.14; // Floating point number
let isStudent: boolean = true; // Boolean value
console.log(`Name: ${name}, Age: ${age}, Pi: ${pi}, Student: ${isStudent}`);2. Data output 📤
To output data to the console, use console.log().
Example:
console.log("Hello, TypeScript!");3. Conditional operators (if...else) 🧐
Allow the program to make decisions.
Example:
let age: number = 20;
if (age >= 18) {
console.log("You're an adult!");
} else {
console.log("You're still a child.");
}4. Loops (for, while) 🔄
Loops allow you to perform the same action multiple times.
Example:
for (let i: number = 1; i <= 5; i++) {
console.log(i);
}5. Functions 📐
Functions help to structure the code.
Example:
function square(x: number): number {
return x * x;
}
console.log("Square of the number 5:", square(5));Simple projects in TypeScript 💡
1. Random number generator 🎲
The program generates a random number from 1 to 100.
function getRandomNumber(): number {
return Math.floor(Math.random() * 100) + 1;
}
console.log("Random number:", getRandomNumber());2. Even numbers filter 🔢
The function filters even numbers from the array.
function filterEvenNumbers(numbers: number[]): number[] {
return numbers.filter(num => num % 2 === 0);
}
console.log(filterEvenNumbers([1, 2, 3, 4, 5, 6]));3. Checking the string for palindromes 🔄
The program checks whether the string is a palindrome.
function isPalindrome(str: string): boolean {
const cleanStr = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleanStr === cleanStr.split('').reverse().join('');
}
console.log("Is 'racecar' a palindrome?", isPalindrome("racecar"));4. Counting words in a line 🔢
The function counts the number of words in a string.
function countWords(text: string): number {
return text.split(" ").length;
}
console.log("Number of words in 'Hello, TypeScript world!':", countWords("Hello, TypeScript world!"));5. Fibonacci sequence 🔢
The program calculates the first 10 numbers of the Fibonacci sequence.
function fibonacci(n: number): number[] {
let fib: number[] = [0, 1];
for (let i = 2; i < n; i++) {
fib.push(fib[i - 1] + fib[i - 2]);
}
return fib;
}
console.log("The first 10 Fibonacci numbers:", fibonacci(10));Conclusion 🎉
TypeScript is a powerful development tool that makes code more reliable. We have analyzed its basics: variables, input/output, conditions, loops and functions. Now you can write your first programs! 🚀
The more you practice, the better your code becomes. Experiment, try new tasks and master programming! Good luck learning TypeScript! 😊
