TypeScript
| Technology | |
|---|---|
![]() Image placeholderPlace the file shown below in the assets/articles folder typescript.webp | |
| Developer | Microsoft |
| First announced | 2012 |
| Language foundation | JavaScript / ECMAScript |
| Type system | Static, structural, gradually adoptable |
| Output | JavaScript |
TypeScript is a programming language developed by Microsoft that adds a static type system to JavaScript. TypeScript source is checked by the compiler and transformed into JavaScript that can run in browsers, servers and other JavaScript environments. It is designed for gradual adoption: existing JavaScript can be introduced into a TypeScript project and types can be added over time.[1]
Why TypeScript was created
As JavaScript applications became larger, teams needed better ways to describe data structures, function contracts and relationships between modules. Dynamic typing is flexible, but many mistakes are only discovered when a particular execution path runs. TypeScript adds a way to describe the intended shape of values and check many mismatches during development. Editor tooling can use the same information for autocomplete, navigation and refactoring.
TypeScript does not replace JavaScript or define a separate runtime. Its syntax is largely a superset of JavaScript, and type annotations are removed during compilation. A TypeScript program therefore relies on JavaScript semantics when it executes. A type declaration does not validate data received from a network, and it does not automatically enforce a rule at runtime.
Type system and inference
TypeScript uses a structural type system. Two values are compatible when their relevant properties and types match, rather than only when they share a declared class name. The compiler can infer many types from assignments and expressions, so developers do not need to annotate every variable. Interfaces and type aliases describe object shapes, while generics allow functions and data structures to preserve relationships between input and output types.
Union types represent values that may take more than one form, and discriminated unions help model states explicitly. Narrowing uses checks such as typeof, property tests or tagged fields to refine a value's type within a block. These mechanisms are useful for API responses and application state, especially when a value can be absent or a request can be in multiple phases.
The any type disables much of the checking for a value, while unknown requires validation before use. Overusing any can make a TypeScript project behave like an unchecked JavaScript project. Stronger configurations, explicit boundary validation and clear module interfaces help preserve the benefit of the type system.
Build process and runtime behaviour
The TypeScript compiler (tsc) can type-check a project and emit JavaScript. Many build tools use a separate transformer for compilation and run type checking as another step. Build configuration determines the target JavaScript version, module format, strictness and library definitions. The correct target depends on supported browsers or runtime versions.
Because types are erased, a declaration such as interface User { id: number } does not check that a JSON response actually contains a numeric id. Runtime schemas or explicit validation are needed at trust boundaries. This separation between compile-time guarantees and runtime checks is one of the most important concepts for TypeScript users.
Libraries, declarations and migration
TypeScript can consume JavaScript libraries through declaration files that describe the library's public API. Many packages include their own types, while others rely on community-maintained definitions. A project can migrate incrementally by enabling JavaScript checking, renaming selected files and gradually replacing broad types with more precise models. Migration should be guided by where type errors are most costly, not by a goal of annotating every line.
Strengths and trade-offs
TypeScript improves maintainability for large codebases, gives editors richer information and makes refactoring safer. It also adds a build or checking step, requires decisions about strictness and can create complex types that are difficult to understand. Types are most valuable when they clarify a system's domain; elaborate type-level programming is not automatically better than a simple interface.
Relationship to JavaScript and web development
TypeScript depends on the JavaScript ecosystem and ultimately runs as JavaScript. It is often used with React and browser APIs, while JavaScript remains the runtime language. A typical web application combines TypeScript for checked application logic, HTML for structure and CSS for presentation.
Further engineering considerations
Project configuration and gradual adoption
A TypeScript project uses a configuration file to describe compiler behaviour, source roots, module handling and the level of type checking. Options such as strict mode make assumptions more explicit, but enabling every check in a mature JavaScript project can reveal many existing issues at once. A gradual migration can begin with boundary modules, shared data structures or code that changes frequently. Teams can then increase coverage as interfaces become clearer and tests provide confidence.
Types are most valuable when they describe meaningful contracts. A type that merely repeats implementation details can make refactoring harder, while a well-designed interface documents what a function expects and guarantees. Runtime validation remains necessary for data arriving from APIs, user input, files or external systems: TypeScript declarations disappear during compilation and cannot verify untrusted values on their own. This distinction is central to secure application design. TypeScript therefore complements, rather than replaces, tests, validation and the JavaScript platform on which the program executes.
See also
- JavaScript – The programming language that powers interactive web pages and a large ecosystem of browser, server and desktop software.
- React – A JavaScript library for building user interfaces from composable components.
- C (programming language) – A general-purpose, statically typed systems programming language created at Bell Labs in the early 1970s.
- C++ – A general-purpose, statically typed language that extends C with classes, templates and zero-overhead abstractions.
- Microsoft – An American technology company known for Windows, Office, Azure and developer tools.
- Rust – A compiled systems programming language that enforces memory safety through ownership and borrowing without a garbage collector.
- CSS – The style sheet language that controls the presentation, layout and responsive behaviour of HTML documents.
- HTML – The standard markup language used to structure documents and applications on the World Wide Web.
References
- ↑ TypeScript Handbook (typescriptlang.org)
- ↑ TypeScript FAQ (github.com)
- ↑ ECMA-262: ECMAScript Language Specification (tc39.es)
