migrate-to-shoehorn
它能做什么
它把使用类型捷径的测试代码,改写为 shoehorn 库中更安全的辅助函数。它适合使用部分测试数据编写 TypeScript 测试的开发者。
适用场景
- ✓你的测试文件使用 as 类型断言来伪造数据的时候。
- ✓你需要部分测试数据,又不想逐个写出所有属性的时候。
- ✓你想把测试中的 as unknown as 强制转换替换掉的时候。
技能链接
https://github.com/mattpocock/skills/blob/main/skills/misc/migrate-to-shoehorn/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请用 shoehorn 替换我们测试文件中的 as 强制转换,让测试保持类型安全。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体安装该库,并把每种测试写法改写为对应的辅助函数,例如部分数据使用 fromPartial,有意构造错误数据使用 fromAny。它只修改测试代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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
npm i @total-typescript/shoehorn
Migration patterns
Large objects with few needed properties
Before:
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:
import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});
as Type → fromPartial()
Before:
getUser({ body: { id: "123" } } as Request);
After:
import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));
as unknown as Type → fromAny()
Before:
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose
After:
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));
When to use each
| Function | Use 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
Gather requirements - ask user:
- What test files have
asassertions causing problems? - Are they dealing with large objects where only some properties matter?
- Do they need to pass intentionally wrong data for error testing?
- What test files have
Install and migrate:
- Install:
npm i @total-typescript/shoehorn - Find test files with
asassertions:grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts" - Replace
as TypewithfromPartial() - Replace
as unknown as TypewithfromAny() - Add imports from
@total-typescript/shoehorn - Run type check to verify
- Install:
常见问题
我可以在正式代码中使用 shoehorn 吗?
不可以。技能说明它只用于测试代码。
fromPartial 有什么作用?
它让你只传入测试需要的字段,同时 TypeScript 仍会检查类型。
fromAny 用来做什么?
它用于测试有意传入与类型不符的数据的情况。
详情
作者的原始描述
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.
repo: mattpocock/skills
热度
- GitHub 星标
- 310k
- 排名
- #121
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …