TypeScript FoundationsLesson 1.2
How to install TypeScript and compile your first file
Node.js requirement, npm install typescript, tsc command, tsconfig.json basics, compiling ts to js, running output
Installing TypeScript
TypeScript runs on Node.js. Install it globally or as a dev dependency:
# Global install
npm install -g typescript
# Per-project (recommended)
npm install --save-dev typescript
npx tsc --versionYour first TypeScript file
Create hello.ts:
const greeting: string = "Hello, TypeScript";
console.log(greeting);Compile and run:
npx tsc hello.ts
node hello.jsTypeScript produces a hello.js file — the types are gone, it's pure JavaScript.
tsconfig.json
For real projects, initialize a config file:
npx tsc --initThis creates tsconfig.json. The three settings you need immediately:
{
"compilerOptions": {
"target": "ES2020",
"strict": true,
"outDir": "./dist"
},
"include": ["src/**/*"]
}target sets the JavaScript version to output. strict enables the full set of type checks — always turn this on. outDir keeps compiled files separate from source.
Run npx tsc from the project root to compile everything in src/.
