migrate-to-shoehorn by mattpocock/skills
npx skills add https://github.com/mattpocock/skills --skill migrate-to-shoehornshoehorn 允许你在测试中传递部分数据,同时让 TypeScript 保持满意。它用类型安全的替代方案取代了 as 断言。
仅用于测试代码。 切勿在生产代码中使用 shoehorn。
在测试中使用 as 的问题:
as unknown as Type)npm i @total-typescript/shoehorn
迁移前:
type Request = {
body: { id: string };
headers: Record<string, string>;
cookies: Record<string, string>;
// ...还有20多个属性
};
it("gets user by id", () => {
// 只关心 body.id 但必须伪造整个 Request
getUser({
body: { id: "123" },
headers: {},
cookies: {},
// ...伪造所有20个属性
});
});
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
迁移后:
import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});
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()迁移前:
getUser({ body: { id: 123 } } as unknown as Request); // 故意使用错误类型
迁移后:
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));
| 函数 | 使用场景 |
|---|---|
fromPartial() | 传递仍能通过类型检查的部分数据 |
fromAny() | 传递故意错误的数据(保持自动补全功能) |
fromExact() | 强制使用完整对象(之后可与 fromPartial 互换) |
as 断言导致了问题?npm i @total-typescript/shoehornas 断言的测试文件:grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"as Type 替换为 fromPartial()as unknown as Type 替换为 fromAny()@total-typescript/shoehorn 添加导入每周安装量
499
代码仓库
GitHub 星标
9.5K
首次出现
2026年2月13日
安全审计
安装于
opencode471
codex468
gemini-cli466
github-copilot462
cursor461
kimi-cli460
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:
as unknown as Type) for intentionally wrong datanpm i @total-typescript/shoehorn
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 } }));
| 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) |
Gather requirements - ask user:
as assertions causing problems?Install and migrate :
npm i @total-typescript/shoehornas assertions: grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"as Type with fromPartial()as unknown as Type with fromAny()Weekly Installs
499
Repository
GitHub Stars
9.5K
First Seen
Feb 13, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
opencode471
codex468
gemini-cli466
github-copilot462
cursor461
kimi-cli460
Vue.js测试最佳实践:Vue 3组件、组合式函数、Pinia与异步测试完整指南
3,700 周安装
Awwwards作品集首页模板 - Next.js + GSAP + Locomotive Scroll 流畅滚动动画实现
353 周安装
移动应用用户获取广告活动优化指南:ASA、Meta、Google UAC投放策略与预算分配
353 周安装
Spring Boot MockMvc控制器单元测试教程:REST API测试与Mockito实战
353 周安装
dev-session:AI开发会话管理工具,支持进度跟踪、检查点提交和持久化学习记录
353 周安装
Claude技能审计工具:自动检测代码过时依赖与API变更,提升开发质量
353 周安装
Pulumi Automation API 教程 - 基础设施即代码自动化编排与部署
353 周安装
@total-typescript/shoehorn