TypeScript Overview
What TypeScript is, why it adds types to JavaScript, and how it catches errors before runtime.
What you'll learn
- What TypeScript is and how it relates to JavaScript
- Why static types catch bugs before your code runs
- How the TypeScript compiler (tsc) transforms .ts files into .js
- Where TypeScript is used: front-end, back-end, and tooling
Concept
What Is TypeScript?
TypeScript is a strongly typed programming language that builds on JavaScript by adding static type annotations. It was created by Microsoft in 2012 and is now one of the most popular languages in the world. Every valid JavaScript program is also a valid TypeScript program — TypeScript is a superset of JavaScript. You can adopt it incrementally: rename your .ts files and add types only where they help.
Why Static Types Matter
JavaScript is dynamically typed, so variable types are determined at runtime. This flexibility is convenient but dangerous: a typo like user.namme instead of user.name will not surface until the code runs, possibly in production. TypeScript checks types at compile time — before the code ever executes. The compiler reports mismatches immediately, turning an entire class of runtime bugs into instant editor warnings.
How TypeScript Works
You write code in .ts (or .tsx for React) files. The TypeScript compiler (tsc) reads those files, type-checks them, and erases all type information to produce plain JavaScript. The types exist only during development; they cost nothing at runtime. This process is called "transpilation" — translating TypeScript into JavaScript while validating correctness.
Example
// TypeScript adds type annotations on top of JavaScript.
let username: string = "alice";
let age: number = 30;
let isActive: boolean = true;
// The compiler catches type errors before you run the code.
// username = 42; // ❌ Error: Type 'number' is not assignable to type 'string'.
// Types are erased at compile time — the output is plain JS.
function greet(name: string): string {
return "Hello, " + name;
}
console.log(greet(username));
Save this as .ts and run tsc to compile. The type annotations (string, number, boolean) are checked by the compiler but removed from the final .js output.
Try it
- JSON to TypeScript
Convert JSON data into TypeScript interfaces — see types in action.
- Code Diff
Compare TypeScript output with its JavaScript compilation.
Common mistakes
The mistake
Thinking TypeScript is a completely different language from JavaScript
The fix
TypeScript is a superset of JavaScript. All valid JS is valid TS. You can start by simply renaming .js to .ts.
The mistake
Believing types run at runtime and slow down your app
The fix
Types are erased during compilation. The output is plain JavaScript with zero runtime type-checking overhead.
Related Resources
Related Cheatsheets
- TypeScript Cheatsheet
Quick reference for TypeScript syntax and common patterns.
Practice TypeScript Overview
4 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.