DevAcademy
LearnTypeScriptTypeScript Setup
BeginnerTypeScript

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 --version

Compiling 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.js

tsconfig.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 --init

A Minimal tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "outDir": "dist",
    "esModuleInterop": true
  },
  "include": ["src"]
}

Common Compiler Options

OptionPurpose
targetWhich JavaScript version to compile down to
strictEnables all strict type-checking options at once
outDirWhere compiled .js files are written
includeWhich 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.

Interview Questions

Quick Quiz

1. Which command compiles a TypeScript file into JavaScript?

2. What does tsconfig.json do?

3. What does the strict compiler option do?