6. TypeScript Foundation
What is TypeScript?
JavaScript is a dynamic language. It allows you to assign a variable as a number, and then later reassign that same variable as a string. While this flexibility is great for small scripts, it becomes a nightmare in large applications, causing silent bugs that only appear when the user clicks a button and crashes the app. TypeScript solves this.
The JavaScript Superset
TypeScript is not an entirely new language that replaces JavaScript. It is a strict syntactical superset of JavaScript. This means that any valid JavaScript code is automatically valid TypeScript code. TypeScript simply adds a new layer on top: Static Typing.
Static Typing vs. Dynamic Typing
In Dynamic Typing (JavaScript), types are checked at runtime (when the browser is actually executing the code). In Static Typing (TypeScript), types are checked at compile time (while you are writing the code in your editor). TypeScript acts as a strict proofreader, throwing a red error in your editor if you try to pass text into a math equation, preventing the bug from ever reaching the browser.
The Compilation Process
Web browsers (like Chrome or Safari) do not understand TypeScript natively. They only understand standard JavaScript, HTML, and CSS. Because of this, TypeScript code must be compiled (often referred to as transpiled) into plain JavaScript before it can run. The TypeScript Compiler (`tsc`) strips away all your type definitions and outputs clean, browser-ready JavaScript.
Knowledge Check
Ready to test your understanding of 6. TypeScript Foundation?