Skip to content

Commit bd756cc

Browse files
feat: 完成stub
1 parent c26d614 commit bd756cc

905 files changed

Lines changed: 3977 additions & 356 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

RECORD.md

Lines changed: 89 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,32 @@
5252
|------|--------|------|
5353
| 初始状态 | ~1800 | 仅缺少 npm 依赖的 TS2307 错误 |
5454
| 补全依赖后 | ~1800 | npm 包已安装,开始处理类型 |
55-
| 当前状态 | **2123** | 类型 stub 已创建,但存在质量待修问题 |
56-
57-
### 当前错误分布
58-
59-
| 错误码 | 数量 | 含义 |
60-
|--------|------|------|
61-
| TS2693 | 727 | `export type X` 被当作值使用(应为 `export const/function`|
62-
| TS2339 | 537 | 属性不存在(类型收窄、unknown 类型) |
63-
| TS2614 | 468 | 模块只有默认导出,但代码用命名导入 |
64-
| TS2322 | 128 | 类型不匹配 |
65-
| TS2345 | 57 | 参数类型不匹配 |
66-
| TS2300 | 34 | 重复标识符(stub 文件中 export 重复) |
67-
| TS2307 | 29 | 仍有缺失模块 |
68-
| TS2305 | 28 | 缺失导出成员 |
69-
| TS2724 | 21 | 导出名称不匹配(如 HookEvent vs HOOK_EVENTS) |
70-
| TS2367 | 17 | 比较类型无交集(`"external" === "ant"` 等) |
71-
| 其他 | ~100 | TS2578/TS2315/TS2365/TS2741 等 |
55+
| 第一轮 stub 生成 | ~2163 | 自动 stub 生成但质量问题多(全用 `export type`|
56+
| **第二轮修复后** | **~1341** | 修复了默认导出、补充命名导出、泛型类型 |
57+
58+
### 当前错误分布(第二轮修复后)
59+
60+
| 错误码 | 数量 | 含义 | 性质 |
61+
|--------|------|------|------|
62+
| TS2339 | 701 | 属性不存在 | **主要是源码级问题**(unknown 344, never 121, {} 52) |
63+
| TS2322 | 210 | 类型不匹配 | 源码级 |
64+
| TS2345 | 134 | 参数类型不匹配 | 源码级 |
65+
| TS2367 | 106 | 比较类型无交集 | 源码级(编译时死代码) |
66+
| TS2307 | 29 | 缺失模块 | 可修但收益小 |
67+
| TS2693 | 13 | type 当值用 | 少量残留 |
68+
| TS2300 | 10 | 重复标识符 | 小问题 |
69+
| 其他 | ~138 | TS2365/TS2554/TS2578/TS2538/TS2698 等 | 混合 |
70+
71+
### 关键发现
72+
73+
剩余 1341 个错误中,**绝大多数(~1200+)是源码级别的类型问题**,不是 stub 缺失导致的:
74+
75+
- `unknown` 类型访问属性 (344) — 反编译产生的 `unknown` 需要断言
76+
- `never` 类型 (121) — 联合类型穷尽后的死路径
77+
- `{}` 空对象 (52) — 空 stub 模块的残留影响
78+
- ComputerUseAPI/ComputerUseInputAPI (42) — 私有包声明不够详细
79+
80+
**继续逐个修复类型错误的投入产出比很低。应该改变方向,尝试直接构建运行。**
7281

7382
---
7483

@@ -126,111 +135,84 @@
126135
- 自动创建 `export type X = any` stub
127136
- 已生成 **1206 个 stub 文件,覆盖 2067 个命名导出**
128137

129-
---
130-
131-
## 三、当前问题分析
132-
133-
### 3.1 TS2693 (727 个) — `export type` 被当作值使用 ⚠️ 关键
134-
135-
**原因**: `scripts/create-type-stubs.mjs` 统一使用 `export type X = any` 生成 stub,但代码中很多地方将导入的名称作为****使用(如调用函数、渲染 JSX 组件等)。
136-
137-
**示例**:
138-
139-
```typescript
140-
// stub 中: export type performLogout = any
141-
// 实际使用: performLogout() // TS2693: only refers to a type, but is being used as a value
142-
```
143-
144-
**修复方案**: 将 `export type X = any` 改为 `declare const X: any``export const X: any`。需要区分哪些是纯类型、哪些是值/函数/组件。
138+
### 2.5 第二轮修复 — 默认导出 & 缺失导出 (本次会话)
145139

146-
### 3.2 TS2614 (468 个) — 模块只有默认导出
140+
#### `scripts/fix-default-stubs.mjs` — 修复 120 个 `export default {} as any` 的 stub 文件
147141

148-
**原因**: 部分 stub 文件使用 `export default {} as any`(早期手动创建),但代码用命名导入 `import { Message } from ...`
142+
- 扫描源码中所有 import 语句,区分 `import type {}` vs `import {}`
143+
- 将纯类型导出为 `export type X = any`,值导出为 `export const X: any = (() => {}) as any`
144+
- **效果**: TS2614 从 632 → 0 (完全消除)
149145

150-
**示例**: `src/types/message.ts` 当前内容为 `export default {} as any`,但代码 `import type { Message } from '../types/message.js'`
146+
#### `scripts/fix-missing-exports.mjs` — 补全空模块的导出成员
151147

152-
**修复方案**: 将默认导出改为命名导出。
148+
- 解析 TS2339 中 `typeof import("...")` 的错误,给 81 个模块添加了 147 个缺失导出
149+
- 解析 TS2305 添加了 10 个缺失导出
150+
- 解析 TS2724 添加了 4 个命名不匹配的导出
151+
- 创建了 2 个新 stub 文件修复 TS2307
153152

154-
### 3.3 TS2300 (34 个) — 重复标识符
153+
#### 泛型类型修复
155154

156-
**原因**: stub 文件中有 `export type X = any` 行,同时源文件中也存在同名定义,造成冲突。部分 stub 文件路径不正确(如 `src/components/CustomSelect/select.ts` 既有真实代码又有 stub 导出)。
155+
将以下类型从非泛型改为泛型(修复 86 个 TS2315):
157156

158-
**修复方案**: 检查这些文件,如果真实文件存在则删除 stub 中的重复导出。
157+
- `DeepImmutable<T>`, `Permutations<T>` (`src/types/utils.ts`)
158+
- `AttachmentMessage<T>`, `ProgressMessage<T>`, `NormalizedAssistantMessage<T>` (`src/types/message.ts` 及其多个副本)
159+
- `WizardContextValue<T>` (`src/components/wizard/types.ts`)
159160

160-
### 3.4 TS2339 (537 个) — 属性不存在
161+
#### 语法修复
161162

162-
**原因**: `any` 类型的 stub 过于宽松时可以正常工作,但部分地方类型收窄后(如联合类型判别、`unknown` 类型)无法访问属性。
163+
修复 4 个 `export const default` 非法语法(buddy/fork/peers/workflows 的 index.ts)
163164

164-
**分类**:
165+
---
165166

166-
- 内部代码中 `unknown` 类型需要类型断言(源码问题)
167-
- `McpServerConfigForProcessTransport` 等类型定义过窄(stub 精度问题)
168-
- `{ ok: true } | { ok: false; error: string }` 联合类型访问 `.error`(源码惯用模式)
167+
## 三、当前问题分析
169168

170-
**修复方案**: 调整相关 stub 类型定义使其更精确。
169+
### 3.1 剩余错误已触达 "源码级" 地板
171170

172-
### 3.5 TS2307 (29 个) — 仍有缺失模块
171+
剩余 ~1341 个错误绝大多数不是 stub 缺失问题,而是源码本身的类型问题:
173172

174-
主要是路径解析问题产生的重复 stub(如 `src/cli/src/` 下的文件已被删除)以及一些深度嵌套的相对路径。
173+
- **unknown (344)**: 反编译代码中大量 `unknown` 类型变量直接访问属性
174+
- **never (121)**: 联合类型穷尽后的 never 路径(通常是 switch/if-else 的 exhaustive check)
175+
- **{} (52)**: 空对象类型
176+
- **类型比较 (106 TS2367)**: 编译时死代码如 `"external" === "ant"`
177+
- **类型不匹配 (210 TS2322 + 134 TS2345)**: stub 类型不够精确
175178

176-
### 3.6 路径问题
179+
### 3.2 继续修类型错误的问题
177180

178-
部分 stub 文件被错误创建在 `src/cli/src/` 等嵌套路径下(因为 `import { X } from 'src/something'``src/cli/handlers/auth.ts` 解析时路径计算错误)。已手动删除部分重复文件,但可能仍有残留。
181+
1. **投入产出比极低** — 每个 TS2339/TS2322 都需要理解具体上下文
182+
2. **Bun bundler 不强制要求零 TS 错误** — Bun 可以在有类型错误的情况下成功构建
183+
3. **大部分是运行时无影响的类型问题**`unknown as any` 等模式不影响实际执行
179184

180185
---
181186

182-
## 四、后续处理方案
183-
184-
### Phase 1: 修复脚本 — 区分类型导出 vs 值导出 (预计解决 ~1200 个错误)
185-
186-
改进 `scripts/create-type-stubs.mjs`
187-
188-
1. **分析 import 语句上下文**
189-
- `import type { X }` → 纯类型,用 `export type X = any`
190-
- `import { X }` (无 type) → 可能是值,用 `export const X: any = (() => {}) as any`
191-
- JSX 组件(大写开头 + 在 JSX 中使用)→ `export const X: React.FC<any> = () => null`
192-
193-
2. **分析使用上下文**
194-
- `X()` 调用 → 函数/值导出
195-
- `<X />` → React 组件
196-
- `X.property` → 对象/命名空间
197-
198-
### Phase 2: 修复默认导出问题 (预计解决 ~468 个 TS2614)
187+
## 四、后续方向建议
199188

200-
将所有 `export default {} as any` 的 stub 文件替换为带命名导出的版本:
189+
### 方向 A: 直接尝试 `bun build` 构建 ⭐ 推荐
201190

202-
```typescript
203-
// 之前
204-
export default {} as any
191+
Bun bundler 对类型错误比 tsc 宽容得多。应该直接尝试构建:
205192

206-
// 之后 — 根据导入需求
207-
export type Message = any
208-
export type NormalizedUserMessage = any
209-
// ... 等
193+
```bash
194+
bun build src/entrypoints/cli.tsx --target=node --outdir=dist
210195
```
211196

212-
### Phase 3: 清理冲突和路径问题 (预计解决 ~34 个 TS2300 + 29 个 TS2307)
213-
214-
1. 检查所有带 `TS2300` 错误的文件,删除与真实代码冲突的 stub
215-
2. 清理 `src/cli/src/``src/components/*/src/` 等错误路径下的 stub 残留
216-
3. 修复 `tsconfig.json` 的 `paths` 配置,确保 `src/*` 映射正确
197+
遇到的构建错误才是真正需要修的问题(缺失模块、语法错误等),而非类型错误。
217198

218-
### Phase 4: 精化关键类型定义 (预计解决 ~100+ 个 TS2322/TS2345)
199+
### 方向 B: 在 tsconfig 中进一步放宽
219200

220-
对高频使用的类型提供更精确的定义:
221-
222-
1. **SDK 消息类型** — 使 `type` 字段为字面量联合类型,而非 `string`
223-
2. **McpServerConfig** — 改为联合类型(stdio | sse | http | sse-ide)
224-
3. **HookInput** 系列 — 添加 `hook_event_name` 字面量类型
225-
4. **PermissionResult** — 改为判别联合 `allow | deny`
201+
```json
202+
{
203+
"compilerOptions": {
204+
"strict": false,
205+
"noImplicitAny": false,
206+
"strictNullChecks": false
207+
}
208+
}
209+
```
226210

227-
### Phase 5: 处理源码级别的类型问题 (需评估)
211+
已经是 `strict: false`,但可以进一步添加 `"skipLibCheck": true`(已有)。
228212

229-
这些是源码本身的问题,不属于 stub 范畴:
213+
### 方向 C: 批量 `// @ts-nocheck`
230214

231-
- `TS2367`: `"external" === "ant"` — 构建时消除的死代码
232-
- `TS2339`: 联合类型属性访问 — 需要类型收窄或断言
233-
- `TS2322`: 类型字面量不匹配(如 `"result"` vs `"result_success"`)
215+
对有大量源码级类型错误的文件加 `// @ts-nocheck`,快速消除错误。
234216

235217
---
236218

@@ -242,13 +224,18 @@ export type NormalizedUserMessage = any
242224
| `src/types/internal-modules.d.ts` | 内部 npm 包类型声明 |
243225
| `src/types/react-compiler-runtime.d.ts` | React compiler runtime |
244226
| `src/types/sdk-stubs.d.ts` | SDK 通配符类型(备用) |
227+
| `src/types/message.ts` | Message 系列类型 stub (39 types) |
228+
| `src/types/tools.ts` | 工具进度类型 stub |
229+
| `src/types/utils.ts` | DeepImmutable / Permutations 泛型 |
245230
| `src/entrypoints/sdk/controlTypes.ts` | SDK 控制类型 stub |
246231
| `src/entrypoints/sdk/runtimeTypes.ts` | SDK 运行时类型 stub |
247232
| `src/entrypoints/sdk/coreTypes.generated.ts` | SDK 核心类型 stub |
248233
| `src/entrypoints/sdk/settingsTypes.generated.ts` | SDK 设置类型 stub |
249234
| `src/entrypoints/sdk/toolTypes.ts` | SDK 工具类型 stub |
250235
| `src/entrypoints/sdk/sdkUtilityTypes.ts` | SDK 工具类型 |
251236
| `scripts/create-type-stubs.mjs` | 自动 stub 生成脚本 |
237+
| `scripts/fix-default-stubs.mjs` | 修复 `export default` stub 为命名导出 |
238+
| `scripts/fix-missing-exports.mjs` | 补全空模块缺失的导出成员 |
252239
| `tsconfig.json` | TypeScript 配置 |
253240
| `package.json` | 依赖配置 |
254241

@@ -265,4 +252,13 @@ npx tsc --noEmit 2>&1 | grep "error TS" | sed 's/.*error //' | sed 's/:.*//' | s
265252

266253
# 重新生成 stub(修复脚本后)
267254
node scripts/create-type-stubs.mjs
255+
256+
# 修复默认导出 stub
257+
node scripts/fix-default-stubs.mjs
258+
259+
# 补全缺失导出
260+
node scripts/fix-missing-exports.mjs
261+
262+
# 尝试构建(下一步)
263+
bun build src/entrypoints/cli.tsx --target=node --outdir=dist
268264
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"build": "bun build src/entrypoints/cli.ts --outdir dist --target bun",
7+
"build": "bun build src/entrypoints/cli.tsx --outdir dist --target bun",
88
"dev": "bun run --watch src/entrypoints/cli.tsx"
99
},
1010
"dependencies": {

scripts/create-type-stubs.mjs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Analyzes TypeScript errors and creates stub modules with proper named exports.
4+
* Run: node scripts/create-type-stubs.mjs
5+
*/
6+
import { execSync } from 'child_process';
7+
import { writeFileSync, existsSync, mkdirSync } from 'fs';
8+
import { dirname, join } from 'path';
9+
10+
const ROOT = '/Users/konghayao/code/ai/claude-code';
11+
12+
// Run tsc and capture errors (tsc exits non-zero on type errors, that's expected)
13+
let errors;
14+
try {
15+
errors = execSync('npx tsc --noEmit 2>&1', { encoding: 'utf-8', cwd: ROOT });
16+
} catch (e) {
17+
errors = e.stdout || '';
18+
}
19+
20+
// Map: resolved file path -> Set of needed named exports
21+
const stubExports = new Map();
22+
// Map: resolved file path -> Set of needed default export names
23+
const defaultExports = new Map();
24+
25+
for (const line of errors.split('\n')) {
26+
// TS2614: Module '"X"' has no exported member 'Y'. Did you mean to use 'import Y from "X"' instead?
27+
let m = line.match(/error TS2614: Module '"(.+?)"' has no exported member '(.+?)'\. Did you mean to use 'import .* from/);
28+
if (m) {
29+
const [, mod, member] = m;
30+
if (!defaultExports.has(mod)) defaultExports.set(mod, new Set());
31+
defaultExports.get(mod).add(member);
32+
continue;
33+
}
34+
35+
// TS2305: Module '"X"' has no exported member 'Y'
36+
m = line.match(/error TS2305: Module '"(.+?)"' has no exported member '(.+?)'/);
37+
if (m) {
38+
const [, mod, member] = m;
39+
if (!stubExports.has(mod)) stubExports.set(mod, new Set());
40+
stubExports.get(mod).add(member);
41+
}
42+
43+
// TS2724: '"X"' has no exported member named 'Y'. Did you mean 'Z'?
44+
m = line.match(/error TS2724: '"(.+?)"' has no exported member named '(.+?)'/);
45+
if (m) {
46+
const [, mod, member] = m;
47+
if (!stubExports.has(mod)) stubExports.set(mod, new Set());
48+
stubExports.get(mod).add(member);
49+
}
50+
51+
// TS2306: File 'X' is not a module
52+
m = line.match(/error TS2306: File '(.+?)' is not a module/);
53+
if (m) {
54+
const filePath = m[1];
55+
if (!stubExports.has(filePath)) stubExports.set(filePath, new Set());
56+
}
57+
58+
// TS2307: Cannot find module 'X'
59+
m = line.match(/^(.+?)\(\d+,\d+\): error TS2307: Cannot find module '(.+?)'/);
60+
if (m) {
61+
const [srcFile, mod] = [m[1], m[2]];
62+
if (mod.endsWith('.md')) continue;
63+
if (!mod.startsWith('.') && !mod.startsWith('src/')) continue;
64+
// Will be resolved below
65+
const srcDir = dirname(srcFile);
66+
const resolved = join(ROOT, srcDir, mod).replace(/\.js$/, '.ts');
67+
if (resolved.startsWith(ROOT + '/') && !existsSync(resolved)) {
68+
if (!stubExports.has(resolved)) stubExports.set(resolved, new Set());
69+
}
70+
}
71+
}
72+
73+
// Also parse actual import statements from source files to find what's needed
74+
import { readFileSync } from 'fs';
75+
const allSourceFiles = execSync('find src -name "*.ts" -o -name "*.tsx"', { encoding: 'utf-8', cwd: ROOT }).trim().split('\n');
76+
77+
for (const file of allSourceFiles) {
78+
const content = readFileSync(join(ROOT, file), 'utf-8');
79+
const srcDir = dirname(file);
80+
81+
// Find all import { X, Y } from 'module'
82+
const importRegex = /import\s+(?:type\s+)?\{([^}]+)\}\s+from\s+['"](.+?)['"]/g;
83+
let match;
84+
while ((match = importRegex.exec(content)) !== null) {
85+
const members = match[1].split(',').map(s => s.trim().split(/\s+as\s+/)[0].trim()).filter(Boolean);
86+
let mod = match[2];
87+
if (!mod.startsWith('.') && !mod.startsWith('src/')) continue;
88+
89+
const resolved = join(ROOT, srcDir, mod).replace(/\.js$/, '.ts');
90+
if (resolved.startsWith(ROOT + '/') && !existsSync(resolved)) {
91+
if (!stubExports.has(resolved)) stubExports.set(resolved, new Set());
92+
for (const member of members) {
93+
stubExports.get(resolved).add(member);
94+
}
95+
}
96+
}
97+
}
98+
99+
// Now create/update all stub files
100+
let created = 0;
101+
for (const [filePath, exports] of stubExports) {
102+
const relPath = filePath.replace(ROOT + '/', '');
103+
const dir = dirname(filePath);
104+
105+
if (!existsSync(dir)) {
106+
mkdirSync(dir, { recursive: true });
107+
}
108+
109+
const lines = ['// Auto-generated type stub — replace with real implementation'];
110+
111+
for (const exp of exports) {
112+
lines.push(`export type ${exp} = any;`);
113+
}
114+
115+
// Check if there are default exports needed
116+
for (const [mod, defs] of defaultExports) {
117+
// Match the module path
118+
const modNorm = mod.replace(/\.js$/, '').replace(/^src\//, '');
119+
const filePathNorm = relPath.replace(/\.ts$/, '');
120+
if (modNorm === filePathNorm || mod === relPath) {
121+
for (const def of defs) {
122+
lines.push(`export type ${def} = any;`);
123+
}
124+
}
125+
}
126+
127+
// Ensure at least export {}
128+
if (exports.size === 0) {
129+
lines.push('export {};');
130+
}
131+
132+
writeFileSync(filePath, lines.join('\n') + '\n');
133+
created++;
134+
}
135+
136+
console.log(`Created/updated ${created} stub files`);
137+
console.log(`Total named exports resolved: ${[...stubExports.values()].reduce((a, b) => a + b.size, 0)}`);

0 commit comments

Comments
 (0)