1. 安装 Node.js
TypeScript 运行在 Node.js 之上。前往 nodejs.org 下载 LTS(长期支持)版本,推荐 Node.js 20+。
💡 推荐使用 nvm(Node Version Manager)管理多版本 Node.js,方便在不同项目间切换。
安装完成后,验证安装:
node -v # 例如 v20.11.0
npm -v # 例如 10.2.4
包管理器选择
Node.js 自带 npm,但你也可以选择更快的替代品:
- npm — 默认包管理器,开箱即用
- pnpm — 更快、节省磁盘空间,推荐:
npm install -g pnpm - yarn — Facebook 出品,也是不错的选择
2. 安装 TypeScript
全局安装 TypeScript 编译器
npm install -g typescript
tsc --version # 例如 Version 5.4.5
直接运行 TS 的工具
每次先编译再运行太繁琐?以下工具可以直接执行 .ts 文件:
| 工具 | 安装 | 特点 |
|---|---|---|
| ts-node | npm install -g ts-node |
经典方案,带类型检查 |
| tsx | npm install -g tsx |
基于 esbuild,启动极快,推荐用于开发 |
🔄 与 Java 类比
TypeScript 的编译过程类似 javac:.ts → tsc → .js,就像 .java → javac → .class。而 tsx / ts-node 则类似 JShell,让你跳过手动编译步骤。
3. Hello World
创建文件 hello.ts:
const greeting: string = "Hello, TypeScript!";
const year: number = 2026;
console.log(`${greeting} 现在是 ${year} 年。`);
方式一:编译后运行
# 编译 .ts → .js
tsc hello.ts
# 运行编译后的 JS
node hello.js
方式二:直接运行(推荐开发时使用)
# 使用 tsx(无需全局安装,npx 会自动下载)
npx tsx hello.ts
# 或者使用 ts-node
npx ts-node hello.ts
💡 npx 可以直接运行 npm 包中的命令,无需全局安装。
4. tsconfig.json 配置
使用以下命令生成默认配置文件:
tsc --init
推荐的 tsconfig.json 配置:
{
"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"]
}
关键配置项说明
| 选项 | 说明 |
|---|---|
| target | 编译输出的 JS 版本。ES2022 覆盖现代 Node.js 和浏览器 |
| module | 模块系统。Node16 支持 ESM 和 CJS 双模式 |
| strict | 启用所有严格类型检查。强烈建议开启,这是 TS 的核心价值 |
| outDir / rootDir | 输出目录和源码根目录,保持项目结构清晰 |
| esModuleInterop | 允许用 import x from 'mod' 导入 CommonJS 模块 |
| resolveJsonModule | 允许直接 import JSON 文件并获得类型推断 |
5. VS Code 配置
VS Code 内置了强大的 TypeScript 支持(语法高亮、智能提示、重构、类型检查),无需额外配置即可使用。
推荐插件
ESLint
代码规范检查,搭配 @typescript-eslint 使用
Prettier
代码格式化工具,保持团队代码风格一致
TypeScript Importer
自动补全 import 语句,提升开发效率
💡 VS Code 底部状态栏显示当前 TypeScript 版本,点击可切换工作区的 TS 版本(项目本地 vs 全局)。
6. 项目脚手架
初始化项目
# 创建项目目录
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
典型目录结构
my-ts-project/
├── src/
│ ├── index.ts # 入口文件
│ └── utils/
│ └── helper.ts
├── dist/ # 编译输出(git忽略)
├── node_modules/ # 依赖(git忽略)
├── package.json
├── tsconfig.json
└── .gitignore
配置 package.json scripts
{
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "tsx watch src/index.ts",
"lint": "eslint src/"
}
}
现在你可以用以下命令进行开发:
npm run dev # 开发模式,文件修改自动重新运行
npm run build # 编译为 JS
npm start # 运行编译后的代码
📋 本章要点
Node.js 是基础
TypeScript 构建在 Node.js 生态之上,安装 Node.js LTS 是第一步。
tsc 是编译器
tsc 将 .ts 编译为 .js;开发时用 tsx 直接运行更高效。
strict 模式必开
tsconfig.json 中 "strict": true 是使用 TypeScript 的核心价值所在。
项目级安装
推荐将 TypeScript 安装为项目的 devDependency,而非仅依赖全局安装。
VS Code 是最佳搭档
VS Code 内置 TypeScript 语言服务,配合 ESLint 和 Prettier 插件,获得最佳开发体验。