Learn TypeScript
TypeScript is a strongly typed language built on top of JavaScript that helps you catch errors before your code ever runs.
What is TypeScript?
TypeScript is an open-source language developed and maintained by Microsoft. It is often described as 'JavaScript with types' because every valid JavaScript program is also a valid starting point for a TypeScript program. TypeScript adds a static type system on top of JavaScript, meaning you can describe the shape of your data - strings, numbers, objects, functions - and the TypeScript compiler will check that your code respects those shapes before it ever runs in a browser or on a server.
Because browsers and Node.js only understand JavaScript, TypeScript code is never executed directly. Instead, it is compiled (or 'transpiled') into plain JavaScript by a tool called the TypeScript compiler, tsc. Once compiled, the output is ordinary JavaScript that runs anywhere JavaScript already runs.
Why use TypeScript?
- Catches type-related bugs at compile time instead of at runtime, when they are far more expensive to find.
- Provides rich autocomplete, inline documentation, and refactoring support in editors like VS Code.
- Makes large codebases easier to navigate because function signatures and object shapes are self-documenting.
- Scales well on teams, since types act as a contract between different parts of an application.
- Supports the newest JavaScript features and compiles them down for older environments if needed.
Example
// Plain JavaScript - no error until the code actually runs
function add(a, b) {
return a + b;
}
console.log(add(5, "10")); // "510" - probably not what you wantedExample
// The same function in TypeScript
function add(a: number, b: number): number {
return a + b;
}
console.log(add(5, 10)); // 15
// add(5, "10"); // Compile-time error: Argument of type 'string'
// is not assignable to parameter of type 'number'.How TypeScript fits into a project
A typical workflow looks like this: you write your application logic in .ts (or .tsx for React) files, using type annotations where they add clarity. The TypeScript compiler reads these files, checks them for type errors, and emits plain .js files. Those .js files are what actually get bundled, shipped, and executed. Many modern frameworks - Angular, Next.js, NestJS - use TypeScript by default.
Exercise: TypeScript Introduction
What is TypeScript, in relation to JavaScript?
Frequently Asked Questions
- Should I learn JavaScript before TypeScript?
- Yes. TypeScript is JavaScript with types added, so every concept in JavaScript still applies. Starting with TypeScript means learning two things at once and confusing which is which.
- Is TypeScript worth learning?
- For anything beyond a small script, yes. Types catch a large class of errors before the code runs and make editor autocomplete far more useful, which matters most on teams and in large codebases.
- Does TypeScript run in the browser?
- Not directly. It compiles to plain JavaScript, which is what actually runs. The types exist only during development and disappear entirely from the output.