Installation
Set up TypeScript in your project.
Install TypeScript
npm install -g typescript
# Verify installation
tsc --version
Initialize Project
mkdir my-ts-project
cd my-ts-project
npm init -y
npx tsc --init
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Compile and Run
# Compile TypeScript to JavaScript
tsc
# Or watch mode (auto-recompile)
tsc --watch
# Run compiled JS
node dist/index.js
ts-node (run TS directly)
npm install -D ts-node
npx ts-node src/index.ts