← Back to Index

Chapter 1: Environment Setup

Install Node.js and TypeScript, configure the development environment

1. Install Node.js

TypeScript runs on top of Node.js. Visit nodejs.org to download the LTS (Long-Term Support) version. Node.js 20+ is recommended.

💡 It's recommended to use nvm (Node Version Manager) to manage multiple Node.js versions, making it easy to switch between projects.

After installation, verify:

node -v    # 例如 v20.11.0
npm -v     # 例如 10.2.4

Package Manager Options

Node.js comes with npm, but you can also choose faster alternatives:

2. Install TypeScript

Install TypeScript Compiler Globally

npm install -g typescript
tsc --version   # 例如 Version 5.4.5

Tools for Running TS Directly

Compiling before running every time is tedious. These tools can execute .ts files directly:

Tool Installation Features
ts-node npm install -g ts-node Classic solution, includes type checking
tsx npm install -g tsx Based on esbuild, extremely fast startup, recommended for development

🔄 Comparison with Java

TypeScript's compilation process is similar to javac: .ts → tsc → .js, just like .java → javac → .class. And tsx / ts-node are similar to JShell, letting you skip the manual compilation step.

3. Hello World

Create a file hello.ts:

const greeting: string = "Hello, TypeScript!";
const year: number = 2026;

console.log(`${greeting} 现在是 ${year} 年。`);

Method 1: Compile then Run

# 编译 .ts → .js
tsc hello.ts

# 运行编译后的 JS
node hello.js

Method 2: Run Directly (Recommended for Development)

# 使用 tsx(无需全局安装,npx 会自动下载)
npx tsx hello.ts

# 或者使用 ts-node
npx ts-node hello.ts

💡 npx can directly run commands from npm packages without global installation.

4. tsconfig.json Configuration

Use the following command to generate a default configuration file:

tsc --init

Recommended tsconfig.json configuration:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "strict": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "declaration": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

Key Configuration Options

Option Description
target JS version for compiled output. ES2022 covers modern Node.js and browsers
module Module system. Node16 supports both ESM and CJS dual mode
strict Enables all strict type checks. Strongly recommended — this is TypeScript's core value
outDir / rootDir Output directory and source root directory, keeps project structure clean
esModuleInterop Allows using import x from 'mod' to import CommonJS modules
resolveJsonModule Allows directly importing JSON files with type inference

5. VS Code Setup

VS Code has built-in powerful TypeScript support (syntax highlighting, IntelliSense, refactoring, type checking), and works without any additional configuration.

Recommended Extensions

ESLint

Code linting, used with @typescript-eslint

Prettier

Code formatter, keeps team code style consistent

TypeScript Importer

Auto-completes import statements, improves development efficiency

💡 The VS Code status bar shows the current TypeScript version. Click to switch between workspace TS version (project local vs global).

6. Project Scaffold

Initialize Project

# 创建项目目录
mkdir my-ts-project && cd my-ts-project

# 初始化 package.json
npm init -y

# 安装 TypeScript 为开发依赖(推荐,而非全局安装)
npm install typescript --save-dev

# 生成 tsconfig.json
npx tsc --init

# 创建目录结构
mkdir src dist

Typical Directory Structure

my-ts-project/
├── src/
│   ├── index.ts          # 入口文件
│   └── utils/
│       └── helper.ts
├── dist/                  # 编译输出(git忽略)
├── node_modules/          # 依赖(git忽略)
├── package.json
├── tsconfig.json
└── .gitignore

Configure package.json scripts

{
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "tsx watch src/index.ts",
    "lint": "eslint src/"
  }
}

Now you can develop with the following commands:

npm run dev    # 开发模式,文件修改自动重新运行
npm run build  # 编译为 JS
npm start      # 运行编译后的代码

📋 Chapter Summary

Node.js is the Foundation

TypeScript is built on the Node.js ecosystem. Installing Node.js LTS is the first step.

tsc is the Compiler

tsc compiles .ts to .js; use tsx for more efficient direct execution during development.

strict Mode is Essential

"strict": true in tsconfig.json is the core value of using TypeScript.

Project-Level Installation

It's recommended to install TypeScript as a project devDependency rather than relying solely on a global install.

VS Code is the Best Companion

VS Code has a built-in TypeScript language service. Combined with ESLint and Prettier extensions, you get the best development experience.