TypeScript vs JavaScript
How TypeScript differs from JavaScript: static types, compile-time checks, and tooling.
What you'll learn
- The key differences between TypeScript and JavaScript
- What compile-time type checking means in practice
- How TypeScript improves developer tooling and refactoring
- When to choose TypeScript over plain JavaScript
Concept
The Core Difference
The fundamental difference is when types are checked. JavaScript checks types at runtime — if you call undefined.foo(), the error only appears when that line executes. TypeScript checks types at compile time — the compiler knows undefined has no method foo and warns you before you ever run the code. This shift from "find bugs in production" to "find bugs in your editor" is the single biggest benefit.
What TypeScript Adds
TypeScript adds: type annotations (let x: number), interfaces and type aliases for describing object shapes, generics for reusable typed code, union and intersection types for flexible modeling, and utility types for transforming existing types. None of these exist in plain JavaScript. The TypeScript compiler also provides far richer autocompletion, inline documentation, and refactoring support.
What TypeScript Does Not Change
TypeScript does not change JavaScript's runtime behavior. There is no type information at runtime — no reflection, no runtime type guards based on TS types. TypeScript also does not make your code faster; the compiled output is the same JavaScript you would have written by hand. It makes you faster and more confident, not the machine.
Example
// JavaScript version — no type safety, bugs surface at runtime.
function jsTotal(items) {
return items.reduce((sum, item) => sum + item.price, 0);
}
// TypeScript version — compiler validates shapes and catches errors.
interface CartItem {
name: string;
price: number;
quantity: number;
}
function tsTotal(items: CartItem[]): number {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}
const cart: CartItem[] = [
{ name: "Book", price: 12.99, quantity: 2 },
{ name: "Pen", price: 1.5, quantity: 5 },
];
console.log(tsTotal(cart)); // 33.48
The JS version accepts anything and fails silently if the shape is wrong. The TS version guarantees every item has name, price, and quantity — the compiler enforces it.
Try it
- JSON to TypeScript
See how JSON shapes become typed TypeScript interfaces.
- Code Diff
Compare typed and untyped versions of the same logic.
Common mistakes
The mistake
Expecting TypeScript to validate types at runtime
The fix
TypeScript types are compile-time only. For runtime validation (e.g. parsing API responses), use a validation library like zod or io-ts.
The mistake
Adding so many types that code becomes hard to read
The fix
Use type inference where possible. Let TypeScript infer types for simple variables and only annotate function signatures and complex types.
Practice TypeScript vs JavaScript
3 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.