TypeScript Setup
Install the TypeScript compiler, create your first .ts file, and configure a project with tsconfig.json.
Reading Time
12 min
Lesson
Lesson 2 of 30
Installing TypeScript
TypeScript is installed via npm, either globally or as a project dependency. Installing it locally per-project is recommended so everyone on a team compiles with the same version.
Installing TypeScript Example
npm install typescript --save-dev
# Check the installed version
npx tsc --versionCompiling a File
The tsc command compiles a .ts file into a .js file. Any type errors are reported in the terminal, and by default a .js file is still emitted even if errors are found (unless configured otherwise).
Compiling Manually
// greet.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("DevAcademy"));Running the Compiler
npx tsc greet.ts
# produces greet.js, which you run normally:
node greet.jstsconfig.json
Real projects use a tsconfig.json file to configure how the compiler behaves — which files to include, which JavaScript version to target, and which strictness rules to enforce.
Generating a tsconfig.json
npx tsc --initA Minimal tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "dist",
"esModuleInterop": true
},
"include": ["src"]
}Common Compiler Options
| Option | Purpose |
|---|---|
| target | Which JavaScript version to compile down to |
| strict | Enables all strict type-checking options at once |
| outDir | Where compiled .js files are written |
| include | Which files/folders the compiler should process |
Editor Support Without Compiling
Editors like VS Code use the TypeScript language service to show type errors and autocomplete live as you type, even before you run tsc — the compile step is mainly for producing the final JavaScript output.
Best Practice
Enable "strict": true from the very start of a new project. Retrofitting strict mode onto a large, loosely-typed codebase later is far more painful than starting strict.