๐ต Why TypeScript & Setup
๐ต Why TypeScript & Setup
The Problem TypeScript Solves
JavaScript is incredibly flexible โ but that flexibility comes with a cost:
JavaScript vs TypeScript
JavaScript
Dynamic types. Errors appear at runtime:
TypeScript
Static types. Errors caught during development:
Key Benefits of TypeScript
- Catch bugs early: Type errors are discovered during development, not in production.
- Better IDE support: Autocomplete, refactoring, and navigation all improve dramatically.
- Self-documenting code: Types tell developers what functions expect and return.
- Scalability: Large codebases stay maintainable as complexity grows.
- Confidence in refactoring: Change code knowing the compiler will catch breaking changes.
- Industry standard: Used by React, Angular, Vue, Node.js frameworks, and more.
โ๏ธ Installation & Setup
This makes the tsc (TypeScript compiler) available anywhere in your terminal.
This creates a package.json file to manage your project's dependencies.
Installing locally (rather than globally) is best practice for team projects.
This creates the tsconfig.json configuration file. Review it โ the defaults are usually fine for beginners.
โ๏ธ Understanding tsconfig.json
tsconfig.json file tells the TypeScript compiler how to behave. Here are the most important settings:
๐ Your First TypeScript Program
This compiles all TypeScript files in src/ to JavaScript in dist/.
Output: Hello, TypeScript!
๐ป Coding Challenges
Challenge 1: Type the Parameters
Add type annotations to this function so TypeScript prevents passing wrong types:
Goal: Make sure add("5", 3) produces a TypeScript error.
Challenge 2: Debug the tsconfig
You've installed TypeScript, but npx tsc gives an error saying it can't find tsconfig.json. What command did you forget?
Goal: Recall the command to generate tsconfig.json.
Challenge 3: Compile and Run
Create a TypeScript file that prints your name in uppercase. Compile it and run the resulting JavaScript file.
Goal: Demonstrate the full workflow: write TS โ compile โ run JS.
The TypeScript compiler is your first line of defense. If it complains about a type error, stop and read the message carefully โ it's trying to protect you from bugs! As you learn, you'll develop a sense for why the compiler is rejecting your code, and you'll start writing types that prevent mistakes naturally.
๐ฏ What's Next
In the next chapter, we'll explore TypeScript's type system: primitives, any, unknown, and how type inference works. You'll learn the vocabulary that makes TypeScript powerful.