TASK-TS-REFACTOR-20250420-011500.md•4.81 kB
# Task: TypeScript Project Refactoring & Optimization
**Goal:** 全面优化 TypeScript 项目,增强类型安全性和可维护性,基于 `project_journal/planning/stack_profile.md` 的分析结果。
**Status:** ⚠️ Completed (with unresolved test issues)
**Coordinator:** TASK-CMD-20250420-011500
**Assigned To:** typescript-specialist
**Acceptance Criteria:**
* `[✅]` `tsconfig.json` 配置了最严格的类型检查 (`strict: true`, `noImplicitAny: true`, `strictNullChecks: true`, `allowJs: false` 等)。
* `[✅]` 代码中尽可能消除了 `any` 类型。
* `[✅]` 核心业务逻辑(如 `src/tools/` 下的功能)具有明确且完善的类型定义(接口、泛型、枚举等),特别关注 `src/types/yeepay.d.ts` 的完善。
* `[✅]` 所有必要的 `@types` 包已安装。
* `[✅]` `tsc` 编译通过,无类型错误。
* `[❌]` 所有现有测试 (`npm test` 或根据 `package.json` 确定) 通过。 (Tests are currently failing due to ESM mocking issues)
* `[✅]` 构建脚本 (`package.json` scripts) 和 CI/CD 配置(如果存在)已更新以适应 TypeScript。
**Context Files:**
* `project_journal/planning/stack_profile.md`
* `tsconfig.json`
* `package.json`
* `src/` 目录
* `tests/` 目录
**Checklist:**
* `[✅]` 分析 `project_journal/planning/stack_profile.md` 了解项目概况和现有配置。
* `[✅]` 检查并更新 `tsconfig.json`,启用所有推荐的严格类型检查选项,确保 `allowJs: false`。 📣
* `[✅]` 检查 `package.json` 依赖,运行 `npm install` 并安装任何缺失的 `@types` 包。 📣
* `[✅]` 审查 `src/` 目录下的代码,识别并消除 `any` 类型,优先处理 `src/tools/` 和 `src/index.ts`。
* `[✅]` 为 `src/types/yeepay.d.ts` 和其他必要的业务逻辑创建/完善接口、泛型、枚举等类型定义。 (Cleaned up `yeepay.d.ts`, core types defined in `src/tools/`)
* `[✅]` 重构代码以利用 TypeScript 高级特性(如 `utility types`, `mapped types`, `conditional types` 等),提高代码质量。 (Used `as const` in `config.ts`)
* `[✅]` 运行 `tsc --noEmit` 命令,系统性地解决所有报告的类型错误。 📣
* `[❌]` 运行项目测试套件 (执行 `package.json` 中定义的测试命令,可能是 `npm test`),确保所有测试通过。 📣 (Tests failed due to ESM mocking issues)
* `[✅]` 检查并根据需要更新 `package.json` 中的构建 (`build`) 和测试 (`test`) 脚本。
* `[✅]` (如果存在 `.github/workflows` 或类似 CI/CD 配置) 检查并更新 CI/CD 流程以正确执行 TypeScript 编译和测试。
* `[⏳]` 提交所有代码和配置更改。 (Pending user/coordinator action)
---
**Summary:**
* **`tsconfig.json`:** Enhanced for stricter type checking (`strict: true`, `noImplicitAny: true`, `strictNullChecks: true`, `allowJs: false`) and added path aliases (`@/*`).
* **Dependencies:** Installed missing `@types` packages (`@types/axios`, `@types/crypto-js`, `@types/dotenv`).
* **Type Safety:** Eliminated `any` types in `src/index.ts` using Zod schema inference. Refined types in `src/tools/` with specific interfaces and type assertions. Improved `src/config.ts` immutability using `as const`.
* **Type Declarations:** Cleaned up `src/types/yeepay.d.ts`, removing redundant/incorrect declarations. Core business types remain defined within `src/tools/`.
* **Compilation:** TypeScript compilation (`tsc --noEmit`) passed successfully.
* **Scripts & CI/CD:** Checked `package.json` scripts (`build`, `test`) and GitHub Actions workflows (`npm-publish.yml`, `semantic-release.yml`); no changes were required.
* **🔴 Unresolved Issue:** Integration tests (`npm test`) consistently fail with `TypeError: mockedCreatePayment.mockClear is not a function`. This indicates a persistent problem with Jest's ESM module mocking in the current configuration (ESM + TypeScript + Babel). Multiple standard mocking strategies (`jest.mock`, dynamic `import`, manual mocks, `spyOn`) and configuration adjustments (`jest.config.js`, `babel.config.cjs`) were attempted without resolving the issue. Further investigation or a different testing strategy (e.g., forcing CommonJS for tests) is required.
**References:**
* `tsconfig.json` (modified)
* `package.json` (modified - added devDependencies)
* `src/index.ts` (modified)
* `src/tools/createPayment.ts` (modified)
* `src/tools/queryPayment.ts` (modified)
* `src/config.ts` (modified)
* `src/types/yeepay.d.ts` (modified)
* `jest.config.js` (modified multiple times)
* `babel.config.cjs` (modified)
* `src/tools/__mocks__/createPayment.ts` (created)
* `src/tools/__mocks__/queryPayment.ts` (created)
* `tests/integration.test.ts` (modified multiple times)