Introduction & Setup
What is TypeScript, why use it, and how to set up your development environment.
Introduction to TypeScript
TypeScript is a superset of JavaScript developed by Microsoft. It adds optional static typing and other features on top of JavaScript, making it easier to build and maintain large-scale applications.
Why TypeScript?
- Static type checking — catch bugs at compile time, not runtime
- Better IDE support — auto-completion, refactoring, inline docs
- Easier to maintain — types serve as documentation
- Modern JS features — use future JavaScript today
TypeScript vs JavaScript
| Feature | JavaScript | TypeScript |
|---|---|---|
| Typing | Dynamic | Static (optional) |
| Compilation | Interpreted | Compiled to JS |
| Error detection | Runtime | Compile time |
| IDE support | Basic | Excellent |
Setting Up
Install TypeScript globally:
npm install -g typescript
tsc -v # Check version
Create your first TypeScript file — sandbox.ts:
let character = "Mario";
let age = 30;
let isBlackBelt = false;
console.log(character, age, isBlackBelt);
TypeScript infers types from the values you assign. Once a variable has a type, you can't reassign it to a different type:
character = "Luigi"; // ✅ OK — still a string
character = 20; // ❌ Error — Type 'number' is not assignable to type 'string'
Key Takeaways
- TypeScript = JavaScript + static types
- It compiles down to plain JavaScript
- Types are inferred automatically from assigned values
- Install with
npm install -g typescript