迁移到 Shoehorn

为什么用 shoehorn?

shoehorn 让你在测试中传入部分数据,同时保持 TypeScript 满意。它用类型安全的替代方案取代 as 断言。

仅限测试代码。 永远不要在生产代码中使用 shoehorn。

测试中使用 as 的问题:

  • 被训练成避免使用它
  • 必须手动指定目标类型
  • 故意传入错误数据时需要双重 asas unknown as Type

安装

npm i @total-typescript/shoehorn

迁移模式

大对象、只需少量属性

之前:

type Request = {
  body: { id: string };
  headers: Record<string, string>;
  cookies: Record<string, string>;
  // ...20 more properties
};

it("gets user by id", () => {
  // Only care about body.id but must fake entire Request
  getUser({
    body: { id: "123" },
    headers: {},
    cookies: {},
    // ...fake all 20 properties
  });
});

之后:

import { fromPartial } from "@total-typescript/shoehorn";

it("gets user by id", () => {
  getUser(
    fromPartial({
      body: { id: "123" },
    }),
  );
});

as TypefromPartial()

之前:

getUser({ body: { id: "123" } } as Request);

之后:

import { fromPartial } from "@total-typescript/shoehorn";

getUser(fromPartial({ body: { id: "123" } }));

as unknown as TypefromAny()

之前:

getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose

之后:

import { fromAny } from "@total-typescript/shoehorn";

getUser(fromAny({ body: { id: 123 } }));

何时使用哪个

函数 用例
fromPartial() 传入仍能通过类型检查的部分数据
fromAny() 传入故意错误的数据(保留自动补全)
fromExact() 强制完整对象(之后可换成 fromPartial)

工作流

  1. 收集需求 — 询问用户:
    - 哪些测试文件中的 as 断言造成了问题?
    - 是否在处理大对象,而只有部分属性重要?
    - 是否需要为错误测试传入故意错误的数据?

  2. 安装并迁移
    - [ ] 安装:npm i @total-typescript/shoehorn
    - [ ] 查找含 as 断言的测试文件:grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"
    - [ ] 将 as Type 替换为 fromPartial()
    - [ ] 将 as unknown as Type 替换为 fromAny()
    - [ ] 添加来自 @total-typescript/shoehorn 的 import
    - [ ] 运行类型检查以验证