0264. tsc --noEmit 只检查不输出
1. 🎯 本节内容
- --noEmit 参数作用
- 使用场景
- 参数组合
- CI/CD 应用
2. 🫧 评价
--noEmit 让 TypeScript 只进行类型检查而不生成输出文件,是 CI/CD 和类型检查的常用选项。
- 只检查类型不生成文件
- CI/CD 中必备选项
- 配合打包工具使用
- 提升检查速度
- 避免不必要的文件生成
3. 🤔 --noEmit 的作用?
--noEmit 告诉 TypeScript 只执行类型检查,不生成任何输出文件。
3.1. 基本用法
bash
# 只检查类型,不生成文件
tsc --noEmit
# 检查指定文件
tsc --noEmit index.ts
# 使用配置文件
tsc --noEmit --project tsconfig.json1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
3.2. 配置文件方式
json
// tsconfig.json
{
"compilerOptions": {
"noEmit": true,
"strict": true
}
}1
2
3
4
5
6
7
2
3
4
5
6
7
bash
tsc1
3.3. 执行过程
text
普通编译(tsc):
1. 解析 TypeScript 代码
2. 执行类型检查
3. 转换为 JavaScript ✅ 生成 .js 文件
4. 生成 sourceMap ✅ 生成 .map 文件
5. 生成声明文件 ✅ 生成 .d.ts 文件
使用 --noEmit:
1. 解析 TypeScript 代码
2. 执行类型检查
3. ❌ 跳过文件生成
4. 退出(返回错误码)1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
3.4. 示例
ts
// index.ts
function greet(name: string): string {
return `Hello, ${name}!`
}
greet(123) // ❌ 类型错误1
2
3
4
5
6
2
3
4
5
6
bash
$ tsc --noEmit
index.ts:5:7 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
5 greet(123);
~~~
Found 1 error in index.ts:51
2
3
4
5
6
7
8
2
3
4
5
6
7
8
text
结果:
✅ 检测到类型错误
❌ 没有生成任何文件1
2
3
2
3
4. 🤔 使用场景?
4.1. CI/CD 类型检查
yaml
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
type-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm install
- run: npm run type-check # tsc --noEmit
build:
runs-on: ubuntu-latest
needs: type-check
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm install
- run: npm run build # Webpack/Vite 构建1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
json
// package.json
{
"scripts": {
"type-check": "tsc --noEmit",
"build": "vite build"
}
}1
2
3
4
5
6
7
2
3
4
5
6
7
4.2. 配合打包工具
text
现代项目架构:
TypeScript → tsc --noEmit ✅ 类型检查
↓
Babel/Webpack/Vite ✅ 转译和打包
↓
生产代码
优势:
- TypeScript 专注类型检查
- 打包工具处理代码转换
- 各司其职,性能更好1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
json
// tsconfig.json(只用于类型检查)
{
"compilerOptions": {
"noEmit": true,
"strict": true,
"module": "esnext",
"moduleResolution": "bundler"
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
4.3. 编辑器集成
json
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "TypeScript Type Check",
"type": "shell",
"command": "tsc",
"args": ["--noEmit", "--watch"],
"isBackground": true,
"problemMatcher": "$tsc-watch"
}
]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
4.4. Git Hooks
json
// package.json
{
"scripts": {
"type-check": "tsc --noEmit"
},
"husky": {
"hooks": {
"pre-commit": "npm run type-check"
}
}
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
bash
# 提交前自动检查类型
git commit -m "feat: add new feature"
# 如果有类型错误,提交被阻止1
2
3
4
2
3
4
5. 🤔 与其他选项配合?
5.1. watch 模式
bash
# 持续监听并检查类型
tsc --noEmit --watch1
2
2
json
// package.json
{
"scripts": {
"dev": "concurrently \"tsc --noEmit --watch\" \"vite\""
}
}1
2
3
4
5
6
2
3
4
5
6
text
效果:
- tsc 持续检查类型
- vite 负责开发服务器
- 两者并行运行1
2
3
4
2
3
4
5.2. 指定项目
bash
# 检查特定项目
tsc --noEmit --project tsconfig.prod.json
# 检查多个项目
tsc --noEmit --project packages/*/tsconfig.json1
2
3
4
5
2
3
4
5
5.3. 跳过库检查
bash
# 跳过 node_modules 中的类型检查(提升速度)
tsc --noEmit --skipLibCheck1
2
2
json
{
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true // ✅ 提升检查速度
}
}1
2
3
4
5
6
2
3
4
5
6
5.4. 增量检查
bash
# 增量类型检查(利用缓存)
tsc --noEmit --incremental1
2
2
json
{
"compilerOptions": {
"noEmit": true,
"incremental": true
}
}1
2
3
4
5
6
2
3
4
5
6
text
优势:
- 首次检查:完整检查
- 后续检查:只检查变化的文件
- 大幅提升速度1
2
3
4
2
3
4
6. 🤔 CI/CD 中的应用?
6.1. 完整 CI 配置
yaml
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
jobs:
type-check:
name: Type Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install Dependencies
run: npm ci
- name: Type Check
run: npm run type-check
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm run lint
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm test
build:
name: Build
runs-on: ubuntu-latest
needs: [type-check, lint, test]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm run build1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
6.2. package.json 脚本
json
{
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "tsc --noEmit --watch",
"lint": "eslint src --ext .ts,.tsx",
"test": "jest",
"build": "vite build"
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
6.3. GitLab CI
yaml
# .gitlab-ci.yml
stages:
- check
- test
- build
type-check:
stage: check
script:
- npm ci
- npm run type-check
lint:
stage: check
script:
- npm ci
- npm run lint
test:
stage: test
script:
- npm ci
- npm test
build:
stage: build
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
6.4. 性能优化
json
// tsconfig.json(CI 优化配置)
{
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true, // ✅ 跳过库检查
"incremental": true, // ✅ 增量检查
"tsBuildInfoFile": ".cache/tsbuildinfo"
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
yaml
# 缓存 tsbuildinfo
cache:
paths:
- node_modules/
- .cache/1
2
3
4
5
2
3
4
5