跳转到内容

migrate-to-shoehorn

shoehorn 让你在测试中传入不完整的数据,同时保持 TypeScript 类型检查通过。它用类型安全的替代方式取代 as 断言。

仅用于测试代码。 不要在生产代码中使用 shoehorn。

测试中的 as 有这些问题:

  • 我们受过训练,不该使用它
  • 必须手动指定目标类型
  • 对于故意传入错误数据的情况,要写双重 as(as 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" },
}),
);
});

迁移前:

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

迁移后:

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

迁移前:

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 添加导入
    • 运行类型检查进行验证

读者可以点名用它,模型也可以自己调用它。它不在 Claude Code 插件里。

把测试中的 as 断言迁移到 @total-typescript/shoehorn,用 fromPartial()、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.
英文 SKILL.md
---
name: migrate-to-shoehorn
description: 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.
---
# 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
| 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
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

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号