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

Quick Example

A type annotation lets the editor (and compiler) catch mistakes before runtime:

Core Concepts

Best Practices

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

References