TypeScript
A superset of JavaScript that adds static types. TypeScript catches type errors at compile time, before they reach production. It compiles to plain JavaScript. The standard for large JS/Node projects. Types are optional and erased at runtime.
- Any JS project beyond a small script.
- Large codebases needing safety and tooling.
- Quick prototypes where types slow you down.
- Environments without a build step.
Definition
TypeScript adds static typing to JavaScript: you annotate variables, parameters, and returns with types, and the compiler (tsc) checks them before running. It compiles to JS by erasing types. Supports interfaces, generics, unions, and inference. Fully interoperable with JS.
Why it exists
JavaScript's dynamic types catch errors at runtime, which is too late. TypeScript catches type errors at compile time, improving safety, tooling (autocomplete, refactoring), and documentation in large codebases.
How it works
Write .ts/.tsx files with type annotations (or let inference handle them). tsc type-checks and compiles to JS (types erased). The output runs in any JS environment. tsconfig.json configures strictness. Types don't exist at runtime.
Scaling limits
Build step adds time. Complex types (advanced generics) can be hard to write. Type definitions for libraries may be missing or wrong. Learning curve for advanced types.
Cost shape
Free and open source.
Alternatives
- JavaScript — No build step but no type safety.
- Flow — Similar but smaller ecosystem.
Failure modes
- any abuse negating the benefits.
- Overly complex types.
- Wrong library type definitions.