SyntaxStudy
Sign Up
TypeScript Introduction to TypeScript
TypeScript Beginner 8 min read

Introduction to TypeScript

TypeScript is a typed superset of JavaScript developed by Microsoft. It adds static type checking, which catches many errors before your code runs. TypeScript compiles to plain JavaScript and runs anywhere JavaScript runs.

TypeScript is now used by default in most modern frameworks including Angular, Next.js, and Deno.

Example
// Install: npm install -g typescript
// Compile: tsc hello.ts
// Run: node hello.js

// Or use ts-node for direct execution:
// npm install -g ts-node
// ts-node hello.ts

// hello.ts
const greeting: string = 'Hello, TypeScript!';
const year: number = 2024;
const isActive: boolean = true;

console.log(`${greeting} (${year})`);

// Without TypeScript, this bug only appears at runtime:
// Without TS: greet(42) — works, but wrong
// With TS: greet(42) — compile error!
function greet(name: string): string {
    return `Hello, ${name}!`;
}

console.log(greet('Alice'));
// console.log(greet(42)); // TypeScript Error: Argument of type 'number'
                           // is not assignable to parameter of type 'string'