What is TypeScript?
TypeScript is a strongly typed programming language that builds on JavaScript by adding optional static type annotations. Developed by Microsoft, it compiles to plain JavaScript and can run anywhere JavaScript runs.
Quick Facts
| Full Name | TypeScript Programming Language |
|---|---|
| Created | 2012 by Microsoft |
| Specification | Official Specification |
How TypeScript Works
TypeScript was released by Microsoft in 2012 to address the challenges of building large-scale JavaScript applications. It adds features like interfaces, enums, generics, and type inference to JavaScript while maintaining full compatibility. TypeScript code is transpiled to JavaScript, allowing it to run in browsers, Node.js, and other JavaScript environments. The type system catches errors at compile time rather than runtime, improving code quality and developer productivity. TypeScript has become the standard for large frontend frameworks like Angular and is widely adopted in the industry.
Key Characteristics
- Superset of JavaScript with static typing
- Compiles to plain JavaScript
- Supports interfaces, enums, and generics
- Type inference reduces annotation burden
- Excellent IDE support and tooling
- Gradual adoption - can mix with JavaScript
Common Use Cases
- Large-scale web applications
- React, Angular, Vue.js projects
- Node.js backend development
- Library and framework development
- Enterprise application development
Example
// Type annotations
let name: string = 'John';
let age: number = 30;
let isActive: boolean = true;
// Interface
interface User {
id: number;
name: string;
email?: string; // optional
}
// Function with types
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
// Generics
function identity<T>(arg: T): T {
return arg;
}
// Type inference
const numbers = [1, 2, 3]; // inferred as number[]
const doubled = numbers.map(n => n * 2);
// Union types
type Status = 'pending' | 'approved' | 'rejected';
// Class with types
class Person {
constructor(
public name: string,
private age: number
) {}
}