Migrate To Shoehorn

作者 mattpocockc55ee46073ed无许可证收录于 2026年10月8日更新于 2026年10月8日

Migrate test files from `as` type assertions to @total-typescript/shoehorn. Use when user mentions shoehorn, wants to replace `as` in tests, or needs partial test data.

AI 生成的概览

将 TypeScript 测试文件中的 as 类型断言迁移为 @total-typescript/shoehorn 的 fromPartial、fromAny 和 fromExact 辅助函数。

功能
该技能指导代理把 TypeScript 测试文件中的 as 类型断言替换为 @total-typescript/shoehorn 包提供的辅助函数。它说明了 as Type 对应 fromPartial()、as unknown as Type 对应 fromAny() 的映射关系,以及何时使用 fromExact(),并配有修改前后的代码示例。它还给出了一套工作流程,涵盖安装、查找含断言的测试文件、执行替换、添加导入并运行类型检查。它仅适用于测试代码,不适用于生产代码。
适用场景
当用户提到 shoehorn、希望从测试中移除 as 断言,或需要在满足 TypeScript 类型检查的前提下传入部分数据或故意错误的数据时使用。它适用于测试文件依赖类型断言处理大型对象的 TypeScript 项目。
运行要求
需要一个包含测试文件的 TypeScript 项目,并能安装 npm 包 @total-typescript/shoehorn;工作流程会使用 grep 和类型检查。该技能不附带脚本,仅包含说明。

Migrate to Shoehorn

Why 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

Install

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" },    }),  );});

as Type → fromPartial()

Before:

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

After:

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

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 } }));

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)

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

来源与署名

来源:mattpocock/skills位于skills/misc/migrate-to-shoehorn提交c55ee46

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架