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
- Install TypeScript:
npm install -D typescript - Initialize configuration:
npx tsc --init - Write a TypeScript file
src/index.ts. - Compile all files:
npx tsc - For development with auto-reload, use
ts-nodeortsxalongside a file watcher.
Tips
- Use
tsc --noEmitto run type checking without generating output files. - Set
outDirin tsconfig.json to keep compiled JavaScript separate from source TypeScript.
Common issues
- Missing
tsconfig.jsoncausestscto use default settings that may not match your target environment. - If
moduleis set toESNextbut Node expects CommonJS, runtime import errors occur.