TypeScript for Beginners

AIGenerováno AI
Dec 5, 2023
5 min čtení
859 reads
Žádná hodnocení
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

  1. Install TypeScript: npm install -g typescript
  2. Create a .ts file
  3. Compile with tsc filename.ts
  4. Run the generated JavaScript

TypeScript is a powerful tool that can significantly improve your JavaScript development experience!

Ohodnoťte tento návod

Přihlásit se pro ohodnocení tohoto návodu

Komentáře (0)

Přihlásit se pro připojení k diskuzi

Scrollujte dolů pro načtení komentářů a hodnocení