TypeScript for Beginners

IAGerado por IA
Dec 5, 2023
5 min de leitura
859 reads
Sem avaliações
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!

Avalie este tutorial

Entrar para avaliar este tutorial

Comentários (0)

Entrar para participar da discussão

Role para baixo para carregar comentários e avaliações