TypeScript Getting Started
TypeScript Getting Started: Installing tsc and Running .ts Files
Getting started with TypeScript requires Node.js and npm. You install the typescript compiler locally inside your project as a dev dependency, generate a tsconfig.json file, and compile .ts files to JavaScript.
Keeping typescript inside each project's package.json ensures every teammate and CI server compiles with the exact same compiler version.
Step-by-Step Local Project Setup
Run these commands inside a new project folder to install TypeScript and create a default configuration file.
npm init -y
npm install -D typescript
npx tsc --init
Next, save a small script in src/greet.ts and compile it with npx tsc:
const developerName: string = "Karim";
const lessonsDone: number = 3;
console.log(`${developerName} completed ${lessonsDone} TypeScript lessons.`);
Run the compiler and execute the generated JavaScript file:
npx tsc
node dist/greet.js
Faster Execution During Development
During daily backend or script development, tools like tsx (npx tsx src/greet.ts) or Node.js 22.6+ (node --experimental-strip-types / native in 23.6+) run .ts files in one step without managing intermediate .js files manually.
Common Mistakes to Avoid
- Installing
typescriptonly globally: Global installs drift across machines; always listtypescriptin projectdevDependencies. - Running
node file.tson older Node versions: Node 20 and earlier cannot parse: stringannotations and throwSyntaxError. - Editing compiled
.jsoutput instead of.ts: Changes insidedist/get overwritten on the nexttscbuild.
Frequently Asked Questions
What does npx tsc --noEmit do?
It runs the full type checker across your project without writing .js files to disk—ideal for CI checks and projects bundled by Vite or Next.js.
How do I enable watch mode while coding?
Run npx tsc --watch (or npx tsc -w) so TypeScript recompiles changed files automatically whenever you save.
Where should compiled JavaScript files live?
Set "rootDir": "./src" and "outDir": "./dist" in tsconfig.json, and add dist/ to .gitignore.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.