TypeScript là **superset** của JavaScript — JS hợp lệ cũng là TS hợp lệ. TS thêm static typing, interface, compile-time checks.
| Tiêu chí | TypeScript | JavaScript |
|---|---|---|
| Kiểu | Strongly typed, OOP, compiled (Microsoft) | Dynamic typed, lightweight, interpreted (Netscape) |
| Chạy server-side | Không trực tiếp (cần compile → JS) | Có (Node.js, Deno, Bun) |
| Data binding | Types, interfaces mô tả data shape | Không có khái niệm này |
| Compilation | Transpile → JS (babel/tsc) | Không cần compile |
| Module | Hỗ trợ module system (ESM, CommonJS) | ES6+ có module, cũ không |
| Optional params | Hỗ trợ function foo(x?: number) |
Không native (dùng default param hoặc arguments) |
Ví dụ
// JavaScript
document.write("Hello World");
// TypeScript
let hello: string = "Hello";
let world: string = "World";
console.log(hello + " from " + world); // Hello from World
Lợi ích TS: Catch bug lúc compile, IDE support tốt, refactor an toàn, documentation tự nhiên qua types.