JS Learning

TypeScript Compilation

Introduction to TypeScriptSection 3 of 3beginner

Topic Overview

Take your time to understand each concept. Practice with the examples below!

TypeScript needs to be "translated" into JavaScript before browsers can understand it. This process is called compilation, and it's easier than you might think!

The Compilation Process:

Think of TypeScript compilation like translating a book from one language to another. The TypeScript compiler (tsc) is your translator!

Step-by-Step Process:

1. Write TypeScript code in .ts files 2. Run the TypeScript compiler (tsc) 3. Compiler checks for type errors 4. If no errors, creates .js files 5. Run the JavaScript files normally

Setting Up TypeScript:

  • Install TypeScript:npm install -g typescript
  • Initialize project:tsc --init (creates tsconfig.json)
  • Compile a file:tsc myfile.ts
  • Watch mode:tsc --watch (auto-compiles on save)

The tsconfig.json File:

This is your project's rulebook - it tells TypeScript how to compile your code:

  • target:which JavaScript version to output (ES5, ES6, etc.)
  • module:how to organize code (CommonJS, ES Modules)
  • strict:enable all strict type checking options
  • outDir:where to put compiled JavaScript files
  • rootDir:where your TypeScript source files are

Modern Development:

Most modern tools (like Vite, webpack, or Next.js) handle TypeScript compilation automatically - you just write .ts files and they do the rest!