Programming Language
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Developed and maintained by Microsoft, TypeScript adds optional static typing to JavaScript, which helps catch errors early through a type system and enables IDE features like autocompletion and code navigation.
TypeScript is a superset of JavaScript, meaning any valid JavaScript code is also valid TypeScript code. TypeScript files use the .ts extension and are transpiled into JavaScript (.js) files that can be run in any environment that supports JavaScript.
// Basic type annotations let name: string = 'John'; let age: number = 30; let isActive: boolean = true; let nullableValue: string | null = null; // Arrays let numbers: number[] = [1, 2, 3, 4, 5]; let strings: Array<string> = ['a', 'b', 'c']; // Tuple let tuple: [string, number] = ['hello', 10]; // Enum enum Color { Red, Green, Blue } let color: Color = Color.Green;
// Interface interface Person { name: string; age: number; email?: string; // Optional property readonly id: number; // Can only be set at creation time greet(): void; } const employee: Person = { name: 'Alice', age: 30, id: 123, greet() { console.log(`Hello, my name is ${this.name}`); } }; // Type alias type Point = { x: number; y: number; }; type ID = string | number; // Union type const point: Point = { x: 10, y: 20 }; const userId: ID = 'abc123';
// Function with parameter and return type annotations function add(a: number, b: number): number { return a + b; } // Arrow function const multiply = (a: number, b: number): number => a * b; // Optional and default parameters function greet(name: string, greeting?: string): string { return `${greeting || 'Hello'}, ${name}!`; } // Generics function identity<T>(arg: T): T { return arg; } const num = identity<number>(42); const str = identity<string>('hello'); // Generic interface interface Container<T> { value: T; } const numberContainer: Container<number> = { value: 42 };
TypeScript is widely used in many contexts:
TypeScript has evolved significantly since its introduction:
TypeScript is actively maintained with regular updates, incorporating features from new ECMAScript standards and adding powerful type system capabilities.
Here are some excellent resources for learning TypeScript:
Technologies that work well with TypeScript: