1
Fundamentals
What is TypeScript, and how does it differ from JavaScript?
TypeScript is a superset of JavaScript — that means everything you can already write in JavaScript also works in TypeScript, and TypeScript adds extra features on top. The biggest addition is static types: you tell TypeScript what kind of data each variable, parameter, and function return should hold, and it checks that the rest of your code uses them correctly.
The problem it solves:
JavaScript only catches type mistakes when the code actually runs. So you write something, ship it, a user clicks a button, and *then* the browser blows up because you called
The two languages side by side:
``
- It doesn't check types at runtime. If your API sends back wrong data, TypeScript can't catch that — type annotations only exist while you're writing code. For data coming from outside, you still need to validate at runtime.
The problem it solves:
JavaScript only catches type mistakes when the code actually runs. So you write something, ship it, a user clicks a button, and *then* the browser blows up because you called
.toUpperCase() on a number. TypeScript catches that same mistake the moment you type it — before the code has ever run.The two languages side by side:
``
ts
// JavaScript — the error only blows up when it runs:
function greet(name) {
return "Hi " + name.toUpperCase();
}
greet(42); // 💥 Runtime crash: "name.toUpperCase is not a function"
// TypeScript — the same mistake is caught while you type:
function greet(name: string) {
return "Hi " + name.toUpperCase();
}
greet(42); // ❌ Compile error: Argument of type 'number' is not assignable to type 'string'.
`
The : string annotation tells TypeScript "this parameter must be a string." When you call greet(42), TypeScript matches that against the annotation and immediately flags it red in your editor.
Other things TypeScript gives you:
- Better autocompletion — your editor knows exactly what fields exist on an object and offers them as you type.
- Safer refactoring — rename a field on a type and TypeScript instantly shows every file that needs updating.
- Self-documenting code — function signatures show exactly what they expect and return, so the code reads like its own documentation.
What TypeScript does NOT do:
- It doesn't run in the browser directly. Before your code ships, the TypeScript compiler (tsc`) strips out all the type annotations and produces plain JavaScript. The browser only ever sees the JavaScript.- It doesn't check types at runtime. If your API sends back wrong data, TypeScript can't catch that — type annotations only exist while you're writing code. For data coming from outside, you still need to validate at runtime.
💡 Plain English: JavaScript is writing an essay in pen — any mistake you made only surfaces when someone reads it aloud at the front of the class (runtime). You're cringing as they stumble over the wrong word.
TypeScript is writing the same essay with spell-check on. As you type each sentence, the editor underlines mistakes in red — wrong word, missing comma, sentence doesn't agree — *before* anyone else sees it. The reader never trips up because the mistakes were fixed while you were still writing.