TypeScript Lesson 2: Setup & tsconfig.json
Let’s install TypeScript and configure a project properly. tsconfig.json is the brain of every TypeScript project.
Installation
# Install TypeScript globally
npm install -g typescript
# Verify:
tsc --version # 5.x.x
# OR use in a project (recommended):
mkdir my-ts-project && cd my-ts-project
npm init -y
npm install -D typescript
npx tsc --init # creates tsconfig.json
tsconfig.json Explained
{
"compilerOptions": {
"target": "ES2022", // output JS version
"module": "commonjs", // module system
"outDir": "./dist", // compiled JS goes here
"rootDir": "./src", // TypeScript source here
"strict": true, // ALWAYS enable this!
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Your First TypeScript File
// src/hello.ts
const message: string = "Hello, TypeScript!";
const count: number = 42;
const active: boolean = true;
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("World"));
// Compile and run:
npx tsc // compiles src/ → dist/
node dist/hello.js // run the compiled JS
// Or use ts-node to run directly:
npm install -D ts-node
npx ts-node src/hello.ts
🏋️ Practice Task
Create a TypeScript project from scratch. Init with npm, install TypeScript, create tsconfig.json with strict:true. Write a src/app.ts file with at least 5 typed variables and 2 typed functions. Compile and run it.
💡 Hint: mkdir src && touch src/app.ts. After writing, run: npx tsc then node dist/app.js