TypeScript: Installing and Configuring TypeScript

Last updated: 2026-08-26

Installing TypeScript takes just one command, but setting up your development environment requires an understanding of a few key concepts—this lesson will guide you through setting up your TypeScript "workspace" from scratch.

1. Install TypeScript

TypeScript is installed via npm (Node.js's package manager). You'll need to have Node.js installed on your computer first.

(1) Check if Node.js is installed

Open the terminal (PowerShell on Windows, Terminal on macOS), and type:

BASH
node --version
npm --version

If you see a version number (such as v18.17.0 and 9.6.3), it means it is already installed. If you receive a "Command not found" error, please go to the official Node.js website to download and install the LTS version first.

(2) Install TypeScript system-wide

BASH
npm install -g typescript

-g indicates a global installation; once installed, you can use the tsc command from any directory.

(3) Verify that the installation was successful

BASH
tsc --version

If you see output similar to Version 5.3.3, it means the installation was successful.

▶ Example: Check the TypeScript installation status

BASH
# Execute the following commands in order,Confirm that the environment is ready
node --version
npm --version
tsc --version

Output:

TEXT 📖 Display only
v18.17.0
9.6.3
Version 5.3.3
💡 Tip: If tsc --version reports that the command was not found, it’s possible that the global npm installation directory hasn’t been added to your system’s PATH. On Windows, try restarting the terminal; on macOS/Linux, reinstall using sudo npm install -g typescript.



2. The first tsconfig.json

tsconfig.json is a configuration file for TypeScript projects that tells the compiler how to process your code. Although simple projects may not need it, almost every TS project in real-world development includes this file.

(1) Quick Generation

In the project directory, run the following:

BASH
tsc --init

This will automatically generate a tsconfig.json file containing all configurable options (most of which are commented out).

(2) Explanation of Key Configuration Options

The newly generated configuration file contains a lot of information, but beginners only need to focus on these few points:

JSON
{
  "compilerOptions": {
    "target": "ES2016",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "skipLibCheck": true
  }
}
Configuration Option Function Recommended Value
target Compiled JS version "ES2016" or "ES2020"
module Module System "commonjs" (Node.js) or "ES2015" (Browser)
strict Enable all strict type checking trueMust be enabled
esModuleInterop Allow default imports for compatibility true
outDir Compilation Output Directory "./dist"
rootDir Source Code Directory "./src"
skipLibCheck Skip third-party library type checking true (Accelerated compilation)
💡 Tip: strict: true is the "master switch" for TypeScript. It enables multiple strict checking options—including noImplicitAny, strictNullChecks, and strictFunctionTypes—all at once. Although you may see more errors initially, these are precisely the issues you should fix. Don’t turn it off—turning off strict means giving up most of TypeScript’s value.

▶ Example: A tsconfig.json file suitable for this tutorial

Output:

TEXT 📖 Display only
JSON structure with compilerOptions: target (ES2020), module (commonjs), strict (true), esModuleInterop (true), outDir (./dist), rootDir (./src), skipLibCheck (true), forceConsistentCasingInFileNames (true). Includes "src/**/*", excludes "node_modules".
JSON
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Output:

TEXT 📖 Display only
JSON configuration defining compiler options (target, module, strict, etc.) and file inclusion/exclusion rules for TypeScript compilation.
📌 Key Point: include specifies which files to include in the compilation (src/**/* refers to all files in the src directory), while exclude excludes directories that do not need to be compiled.



3. VS Code Configuration

VS Code and TypeScript are "siblings"—both are Microsoft products, and VS Code supports TypeScript right out of the box, without the need to install any additional extensions.

(1) Built-in Features

VS Code comes with built-in TypeScript language support, which provides:

Plugin Function
TSLint (or ESLint + TypeScript plugin) Code style checking, identifying potential issues
Prettier Automatically formats code and standardizes team coding style
Code Runner Run TS files directly within the editor (automatic compilation and execution)

(3) Configure Automatic Compilation

In VS Code, press Ctrl+Shift+B (Cmd+Shift+B on macOS), select tsc: watch, and VS Code will monitor file changes in the background—every time you save the .ts file, it will automatically compile it into .js, so you don't need to run tsc manually.

💡 Tip: The tsc: watch mode is like an "automatic translator"—you type a sentence in Chinese, and it instantly translates it into English for you. Keep it on at all times while you're developing, and your efficiency will double.



4. Project Directory Structure

A standard TypeScript project looks like this:

TEXT 📖 Display only
my-project/
├── src/              ← TypeScript Source Code
│   ├── index.ts      ← Input File
│   └── utils.ts      ← Tools Module
├── dist/             ← Compilation output JavaScript(Automatically Generated,Do not edit manually)
│   ├── index.js
│   └── utils.js
├── tsconfig.json     ← TypeScript Layout
├── package.json      ← Node.js Project Configuration
└── node_modules/     ← Dependency Packages(Automatically Generated,Do not submit to git)
⚠️ Note: The dist/ directory is automatically generated by the compiler; never manually modify the files inside it. Even if you do, it won’t make a difference—they’ll be overwritten the next time you compile. All changes should be made in the .ts file located in src/.



5. Troubleshooting Common Installation Issues

Problem Cause Solution
tsc Command not found npm is not in your PATH Restart the terminal; or use npx tsc instead
npm install -g Permission error macOS/Linux requires sudo sudo npm install -g typescript
VS Code does not display type errors The directory containing tsconfig.json is not open Open the project root directory using the "Open Folder" option
Compilation output is in the wrong location outDir is configured incorrectly Check outDir and rootDir in tsconfig.json
Chinese comments appear as garbled characters after compilation The file encoding is not UTF-8 In VS Code, select "Save with Encoding" in the bottom-right corner → UTF-8

▶ Example: Compiling and Running TypeScript with tsc

TYPESCRIPT
// src/hello.ts
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("TypeScript"));
▶ Try it Yourself
BASH
# Compile and run
tsc src/hello.ts          # Generates dist/hello.js
node dist/hello.js        # Output: Hello, TypeScript!

Output:

TEXT 📖 Display only
tsc src/hello.ts          → Compiles to dist/hello.js
node dist/hello.js        → Hello, TypeScript!
💡 Tip: Use tsc --watch (or tsc -w) to automatically recompile on file changes. Combine with node --watch (Node 18+) for a full auto-reload workflow.


❓ FAQ

Q Do I have to install TypeScript globally?
A No, you don’t have to. You can also install it locally in your project: npm install typescript --save-dev, and then use npx tsc instead of tsc. The advantage of a local installation is that the project team uses a unified version of TypeScript, avoiding issues like “it compiles here but not there.” However, a global installation is more convenient when you’re just learning.
Q Do I have to enable strict in tsconfig.json?
A We strongly recommend enabling it. Strict mode helps you catch the most type errors. Although you’ll see more red wavy underlines at first, each one indicates a real issue. Turning off strict mode is like buying a car but not wearing a seatbelt—it works, but it’s not safe.
Q Do I have to use VS Code as my editor?
A No, you don’t have to. WebStorm, Sublime Text, and other editors also support TypeScript. However, VS Code is free and offers the most comprehensive support for TypeScript (since it’s from the same company), so this tutorial uses VS Code as its standard.
Q Should the dist directory be committed to Git?
A No. dist contains the build artifacts; anyone with the source code can regenerate them by running tsc. Simply add dist to .gitignore.

📖 Summary

📝 Exercises

  1. Basic Exercise (Difficulty: ⭐): Install TypeScript on your computer, run tsc --version to verify that the installation was successful, and make a note of the version number.
  2. Advanced Problem (Difficulty ⭐⭐): Create a project directory, run tsc --init to generate tsconfig.json, then set strict to true, outDir to "./dist", and rootDir to "./src".
  3. Challenge (Difficulty: ⭐⭐⭐): Open the project directory you created in the previous step in VS Code, configure the tsc: watch auto-compilation mode, and try modifying the target value in tsconfig.json (changing it from ES2020 to ES5). Observe how the generated JS code changes (Hint: Arrow functions will become regular functions).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏