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:
function greet(name) { return "Hello, " + name.toUpperCase(); } // โ No error until runtime! greet(42); // TypeError: name.toUpperCase is not a function
TypeScript
Static types. Errors caught during development:
function greet(name: string) { return "Hello, " + name.toUpperCase(); } // โ Error caught immediately! greet(42); // โ Argument of type 'number' is not assignable to parameter of type 'string'
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
npm install -g typescript
This makes the tsc (TypeScript compiler) available anywhere in your terminal.
mkdir my-typescript-project cd my-typescript-project
npm init -y
This creates a package.json file to manage your project's dependencies.
npm install --save-dev typescript
Installing locally (rather than globally) is best practice for team projects.
npx tsc --init
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:
{
"compilerOptions": {
"target": "ES2020", // JavaScript version to compile to
"module": "commonjs", // Module system (commonjs for Node.js)
"lib": ["ES2020"], // Built-in types available
"outDir": "./dist", // Where compiled .js files go
"rootDir": "./src", // Where your .ts files are
"strict": true, // Enable strict type checking
"esModuleInterop": true, // Better CommonJS compatibility
"skipLibCheck": true, // Skip type checking of declaration files
"forceConsistentCasingInFileNames": true
},
"include": ["src"], // Include src directory
"exclude": ["node_modules"] // Exclude node_modules
}
๐ Your First TypeScript Program
mkdir src
// Create: src/hello.ts
const message: string = "Hello, TypeScript!"; console.log(message);
npx tsc
This compiles all TypeScript files in src/ to JavaScript in dist/.
node dist/hello.js
Output: Hello, TypeScript!
๐ป Coding Challenges
Challenge 1: Type the Parameters
Add type annotations to this function so TypeScript prevents passing wrong types:
function add(a, b) { return a + b; }
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.