TypeScript
In short
TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
What is TypeScript?
TypeScript is JavaScript with type annotations. You can declare that a variable holds a string, that a function returns a number, or that an object must have certain properties, and the TypeScript compiler checks that your code follows those rules. Almost any valid JavaScript is also valid TypeScript, so projects can adopt it gradually.
Types exist only while you develop. The compiler, tsc, or a build tool strips them out and produces ordinary JavaScript that browsers and servers can run. Some runtimes, including Deno, Bun, and recent versions of Node.js, can run TypeScript files directly by removing the types on the fly.
Think of types as labels on moving boxes: they don't change what is inside, but they warn you right away if the kitchen plates are headed for the bathroom. The same checking powers editor features like autocomplete, safe renaming, and inline documentation, which is a big reason TypeScript is popular on large codebases.
A common misunderstanding is that TypeScript checks data while the program runs. It does not: after compilation the types are gone, so data from users or APIs still needs to be validated in code, often with a validation library.
Key takeaways
- TypeScript is JavaScript plus optional static types.
- Type errors are reported before the code runs.
- Types are removed at build time, leaving plain JavaScript.
- It enables better autocomplete and refactoring in editors.
- Types do not validate external data at runtime.
Example
// Describe the shape of a user object
interface User {
id: number;
name: string;
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
greet({ id: 1, name: "Ada" }); // OK
greet({ id: 2 }); // Error: property 'name' is missingReaders ask
Is TypeScript better than JavaScript?
Neither is better in every case. TypeScript adds safety and tooling that pay off in larger or long-lived projects, while plain JavaScript can be quicker for small scripts and prototypes.
Do browsers run TypeScript?
No. Browsers only run JavaScript, so TypeScript code is compiled or stripped of its types before it is sent to the browser.
What is the difference between interface and type in TypeScript?
Both describe the shape of data. An interface can be extended and merged across declarations, while a type alias can also express unions, tuples, and other combinations; for plain object shapes, either works.
Often compared
See also
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- FrameworkProgramming Fundamentals, p. 20A framework is a reusable foundation of code, tools, and conventions that provides the structure of an application, so developers only fill in their own logic.
- EnumProgramming Fundamentals, p. 18An enum is a data type that defines a fixed set of named values, such as an order's possible statuses, so code can't use a value outside that set.
Spotted a mistake or something missing on this page?Suggest an edit