Skip to content
TypeScript Essentials

Introduction to TypeScript

Beginner Lesson 1 of 9

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

  • Type Safety - Catch errors at compile time
  • Better IDE Support - Autocomplete, refactoring, navigation
  • Self-Documenting - Types serve as documentation
  • Scalability - Easier to maintain large codebases
Terminal window
# Install TypeScript
npm install -g typescript
# Initialize project
tsc --init
# Compile TypeScript
tsc file.ts
# Watch mode
tsc --watch
// Primitives
let name: string = "Alice";
let age: number = 25;
let isActive: boolean = true;
// Arrays
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ["Alice", "Bob"];
// Tuple
let tuple: [string, number] = ["Alice", 25];
// Any (avoid when possible)
let anything: any = "could be anything";
// Unknown (safer than any)
let unknown: unknown = 4;
// TypeScript infers the type
let message = "Hello"; // string
let count = 42; // number
// No need to annotate when type is obvious
const user = {
name: "Alice",
age: 25
}; // { name: string; age: number }
// Parameter and return types
function greet(name: string): string {
return `Hello, ${name}!`;
}
// Optional parameters
function greet(name: string, greeting?: string): string {
return `${greeting || "Hello"}, ${name}!`;
}
// Default parameters
function greet(name: string, greeting: string = "Hello"): string {
return `${greeting}, ${name}!`;
}
// Arrow functions
const add = (a: number, b: number): number => a + b;

Continue to TypeScript Setup →