Installing TypeScript
TypeScript requires Node.js and npm. Once you have those installed, you can install TypeScript globally or as a project dependency.
Example
# Install TypeScript globally
npm install -g typescript
# Check version
tsc --version
# Or install as dev dependency
npm init -y
npm install --save-dev typescript Your First TypeScript File
TypeScript files use the .ts extension. You write TypeScript code, then compile it to JavaScript using the tsc command.
Example
// hello.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("World"));
// Compile: tsc hello.ts
// Run: node hello.js tsconfig.json
The tsconfig.json file configures how TypeScript compiles your code. Create one with tsc --init.
Example
// Generate tsconfig.json
// tsc --init
// Basic tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
} 