TypeScript vs JavaScript
TypeScript is JavaScript plus type annotations that are checked before the code runs and then removed, so the program that runs is plain JavaScript.
Separate checks made before execution from behavior that still runs as JavaScript.
You write task.titel instead of task.title. JavaScript says nothing: the page loads, and much later a user sees undefined where a task name should be. TypeScript would have underlined that typo the moment you typed it. That early warning is the whole idea.
The rule
TypeScript is JavaScript plus types, checked first and then erased.
Three parts, three ideas:
- JavaScript plus types. Every valid JavaScript program is also valid TypeScript syntax. TypeScript adds annotations that say what kind of value something holds.
- Checked first. Before the code runs, the TypeScript compiler reads those annotations and reports mistakes: a missing property, a number passed where a string belongs, a function called with too few arguments.
- Then erased. The compiler removes the annotations and writes plain JavaScript. Browsers and Node.js run that output; they never see a type.
Here is the task we will model all course long:
type Task = { id: number; title: string; done: boolean };
function formatTask(task: Task): string {
return `${task.id}. ${task.title}`;
}
formatTask({ id: 1, title: "Buy milk", done: false }); // fine
formatTask("Buy milk"); // error: a string is not a TaskAfter compiling, what runs is simply:
function formatTask(task) {
return `${task.id}. ${task.title}`;
}The type Task line and every : Task have vanished. So types cost nothing at runtime, and they also protect nothing at runtime: if bad data arrives from a server while the program runs, TypeScript is no longer there to stop it.

