How to compile TypeScript to JavaScript

· Category: TypeScript

Short answer

Install TypeScript globally or locally with npm, then run tsc to compile .ts files into .js files based on your tsconfig.json settings.

Steps

  1. Install TypeScript: npm install -D typescript
  2. Initialize configuration: npx tsc --init
  3. Write a TypeScript file src/index.ts.
  4. Compile all files: npx tsc
  5. For development with auto-reload, use ts-node or tsx alongside a file watcher.

Tips

  • Use tsc --noEmit to run type checking without generating output files.
  • Set outDir in tsconfig.json to keep compiled JavaScript separate from source TypeScript.

Common issues

  • Missing tsconfig.json causes tsc to use default settings that may not match your target environment.
  • If module is set to ESNext but Node expects CommonJS, runtime import errors occur.