TypeScript for Beginners
ИИСгенерировано ИИ
Dec 5, 2023
5 мин чтения
859 reads
Нет оценок
programming
TypeScript for Beginners
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds optional static typing to JavaScript, which can help catch errors during development.
Why TypeScript?
- Type Safety: Catch errors at compile time
- Better IDE Support: Enhanced autocomplete and refactoring
- Improved Documentation: Types serve as inline documentation
- Easier Refactoring: Confident code changes
Basic Types
Primitive Types
let isDone: boolean = false; let decimal: number = 6; let color: string = "blue";
Arrays
let list: number[] = [1, 2, 3]; let list2: Array<number> = [1, 2, 3];
Interfaces
interface User { name: string; age: number; email?: string; // Optional property } function greet(user: User) { console.log(`Hello, ${user.name}!`); }
Functions
function add(x: number, y: number): number { return x + y; } // Arrow function const multiply = (x: number, y: number): number => x * y;
Getting Started
- Install TypeScript:
npm install -g typescript - Create a .ts file
- Compile with
tsc filename.ts - Run the generated JavaScript
TypeScript is a powerful tool that can significantly improve your JavaScript development experience!