migrate-to-shoehorn
把测试迁移到 Shoehorn
Section titled “把测试迁移到 Shoehorn”为什么用 shoehorn?
Section titled “为什么用 shoehorn?”shoehorn 让你在测试中传入不完整的数据,同时保持 TypeScript 类型检查通过。它用类型安全的替代方式取代 as 断言。
仅用于测试代码。 不要在生产代码中使用 shoehorn。
测试中的 as 有这些问题:
- 我们受过训练,不该使用它
- 必须手动指定目标类型
- 对于故意传入错误数据的情况,要写双重
as(as unknown as Type)
npm i @total-typescript/shoehorn需要少数属性的大型对象
Section titled “需要少数属性的大型对象”迁移前:
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 Type → fromPartial()
Section titled “as Type → fromPartial()”迁移前:
getUser({ body: { id: "123" } } as Request);迁移后:
import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));as unknown as Type → fromAny()
Section titled “as unknown as Type → fromAny()”迁移前:
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose迁移后:
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));何时使用各函数
Section titled “何时使用各函数”| 函数 | 使用场景 |
|---|---|
fromPartial() |
传入仍能通过类型检查的部分数据 |
fromAny() |
传入故意错误的数据(保留自动补全) |
fromExact() |
强制传入完整对象(之后可换成 fromPartial) |
-
收集需求 - 询问用户:
- 哪些测试文件中的
as断言造成了问题? - 是否在处理大型对象,且只关心其中一些属性?
- 是否需要传入故意错误的数据来测试错误处理?
- 哪些测试文件中的
-
安装并迁移:
- 安装:
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添加导入 - 运行类型检查进行验证
- 安装:
原文链接和 commit
Section titled “原文链接和 commit”读者可以点名用它,模型也可以自己调用它。它不在 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.- 仓库 mattpocock/skills
- 文件
skills/misc/migrate-to-shoehorn/SKILL.md - 提交
d81f3a1 - 原仓库里这一则没有单独的说明页。
- 同目录文件
skills/misc/migrate-to-shoehorn/agents/openai.yaml
英文 SKILL.md
---name: migrate-to-shoehorndescription: 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
```bashnpm i @total-typescript/shoehorn```
## Migration patterns
### Large objects with few needed properties
Before:
```tstype 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:
```tsimport { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => { getUser( fromPartial({ body: { id: "123" }, }), );});```
### `as Type` → `fromPartial()`
Before:
```tsgetUser({ body: { id: "123" } } as Request);```
After:
```tsimport { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));```
### `as unknown as Type` → `fromAny()`
Before:
```tsgetUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose```
After:
```tsimport { 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