migrate-to-shoehorn

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Migrate to Shoehorn

迁移至Shoehorn

Why shoehorn?

为什么选择Shoehorn?

shoehorn
lets you pass partial data in tests while keeping TypeScript happy. It replaces
as
assertions with type-safe alternatives.
Test code only. Never use shoehorn in production code.
Problems with
as
in tests:
  • Trained not to use it
  • Must manually specify target type
  • Double-as (
    as unknown as Type
    ) for intentionally wrong data
shoehorn
允许你在测试中传入部分数据,同时保持TypeScript类型校验正常。它用类型安全的替代方案取代了
as
断言。
仅用于测试代码。 切勿在生产代码中使用shoehorn。
测试中使用
as
的问题:
  • 通常被要求避免使用它
  • 必须手动指定目标类型
  • 对于故意传入的错误数据需要使用双重断言(
    as unknown as Type

Install

安装

bash
npm i @total-typescript/shoehorn
bash
npm i @total-typescript/shoehorn

Migration patterns

迁移模式

Large objects with few needed properties

仅需少量属性的大型对象

Before:
ts
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
  });
});
After:
ts
import { fromPartial } from "@total-typescript/shoehorn";

it("gets user by id", () => {
  getUser(
    fromPartial({
      body: { id: "123" },
    }),
  );
});
之前:
ts
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
  });
});
之后:
ts
import { fromPartial } from "@total-typescript/shoehorn";

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

as Type
fromPartial()

as Type
替换为
fromPartial()

Before:
ts
getUser({ body: { id: "123" } } as Request);
After:
ts
import { fromPartial } from "@total-typescript/shoehorn";

getUser(fromPartial({ body: { id: "123" } }));
之前:
ts
getUser({ body: { id: "123" } } as Request);
之后:
ts
import { fromPartial } from "@total-typescript/shoehorn";

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

as unknown as Type
fromAny()

as unknown as Type
替换为
fromAny()

Before:
ts
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose
After:
ts
import { fromAny } from "@total-typescript/shoehorn";

getUser(fromAny({ body: { id: 123 } }));
之前:
ts
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose
之后:
ts
import { fromAny } from "@total-typescript/shoehorn";

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

When to use each

何时使用各函数

FunctionUse case
fromPartial()
Pass partial data that still type-checks
fromAny()
Pass intentionally wrong data (keeps autocomplete)
fromExact()
Force full object (swap with fromPartial later)
函数名使用场景
fromPartial()
传入仍能通过类型校验的部分数据
fromAny()
传入故意构造的错误数据(仍保留自动补全功能)
fromExact()
强制传入完整对象(后续可替换为fromPartial)

Workflow

工作流程

  1. Gather requirements - ask user:
    • What test files have
      as
      assertions causing problems?
    • Are they dealing with large objects where only some properties matter?
    • Do they need to pass intentionally wrong data for error testing?
  2. Install and migrate:
    • Install:
      npm i @total-typescript/shoehorn
    • Find test files with
      as
      assertions:
      grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"
    • Replace
      as Type
      with
      fromPartial()
    • Replace
      as unknown as Type
      with
      fromAny()
    • Add imports from
      @total-typescript/shoehorn
    • Run type check to verify
  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
      中的相关函数
    • 运行类型校验以验证结果