Introduction to TypeScript
TypeScript is an open-source, strongly typed programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, meaning every valid JavaScript program is also technically a valid TypeScript program.
The main philosophy behind TypeScript is simple: Add static typing to JavaScript.
Why Type?
Vanilla JavaScript utilizes Dynamic Typing. This means types are checked dynamically at runtime just as the code executes. You can swap a variable from a Number, to a String, to an Array natively without issue.
While dynamic typing is flexible, it introduces catastrophic runtime errors in large-scale applications where data shapes get confusing.
TypeScript utilizes Static Typing. Types are checked during the compilation phase, meaning potential bugs are caught securely in your code editor before you ever run the application!
Running TypeScript
Browsers and Node.js cannot natively execute TypeScript .ts files.
TypeScript must be globally installed and then compiled (transpiled) down into regular vanilla JavaScript .js files using the tsc compiler command.
# Global install
npm install -g typescript
# Run compilation on a file
tsc index.ts