TypeScript
TypeScript is a typed superset of JavaScript: every valid JS program is valid TS, but you can add static types that the compiler checks before the code ever runs. It compiles down to plain JavaScript, so it runs anywhere JS does — browsers, Node, edge runtimes.
Its value grows with codebase size. Types catch a whole class of bugs at compile time, document intent inline, power autocomplete and safe refactoring, and make large team projects tractable — which is why most modern JavaScript projects adopt it.
TL;DR
- A superset of JavaScript with optional static types; compiles to plain JS.
- Catches bugs at compile time and supercharges editor tooling.
- Core tools: interfaces/types, generics, union types, inference.
- Turn on
strictmode and avoidanyto get the real benefits.
Quick Example
A type annotation lets the editor (and compiler) catch mistakes before runtime:
Core Concepts
- Types & annotations —
string,number, arrays, tuples, and your own shapes. - Interfaces vs type aliases — both describe object shapes;
typealso does unions/primitives. - Union types —
type Role = "admin" | "user" | "guest"models exact possibilities. - Generics — reusable, type-safe components:
function first<T>(arr: T[]): T | undefined. - Inference — TypeScript infers most types, so you annotate boundaries, not everything.
Best Practices
- Enable
strictmode (strict: true) — it's where most of the safety comes from. - Avoid
any— it disables checking; preferunknownand narrow it. - Let inference do the work; annotate function signatures and public APIs.
- Validate external data at runtime (e.g. Zod) — types vanish after compilation.
Common Mistakes
Reaching for any
Trusting types at runtime
FAQ
Why use TypeScript over plain JavaScript?
For anything beyond a small script: it catches type errors before runtime, makes refactoring safe, and gives far better autocomplete and inline docs. The cost is a build step and learning the type system — worth it for team and long-lived codebases.
Interface or type alias?
Use interface for object shapes (it's extendable and gives clearer errors); use type for unions, intersections, primitives, and tuples. In practice they overlap heavily — pick one convention and stay consistent.
Does TypeScript catch all bugs?
No. It catches type errors (wrong shapes, missing fields, bad arguments), not logic errors. A function can be perfectly typed and still compute the wrong answer. Types narrow the bug surface; they don't replace tests.
Do types exist at runtime?
No — types are erased during compilation, leaving plain JavaScript. That's why you must validate external input (API responses, form data) at runtime with a schema library; a as User assertion is just a compile-time promise.
Related Topics
- JavaScript — The language TypeScript builds on
- TypeScript Basics — Fundamentals in depth
- TypeScript Advanced Features — Narrowing, conditional & mapped types
- React — Heavily TypeScript-based
- Forms & Validation — Runtime validation with schemas