迁移到鞋拔子

工程开发 社区

技能解读

设计思路

migrate-to-shoehorn 是 mattpocock 的「测试代码 as 断言升级」专项 skill——把测试里 as Type / as unknown as Type 替换成 @total-typescript/shoehorn 的 fromPartial() / fromAny(),让 TS 仍然开心、又不必虚构整份大对象。只在测试代码里用,永不进生产。

为什么要换 shoehorn

as 在测试里的痛点:

  • 团队规约通常禁用 as
  • 必须手动指定目标类型
  • 故意传错数据时还得 as unknown as Type 双断言

三个迁移模式

1. 大对象只关心少数属性

之前要塞 20 个伪属性才让 TS 闭嘴:

getUser({
  body: { id: "123" },
  headers: {},
  cookies: {},
  // ...还有一堆
});

之后:

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

2. as Type → fromPartial() 直接替换。

3. as unknown as Type → fromAny() 故意传错类型时用 fromAny——保留 autocomplete。

选哪个

函数 适用场景
fromPartial() 传部分数据但仍需 type-check
fromAny() 故意传错数据(错误路径测试)
fromExact() 强制完整对象(以后想换回 fromPartial 时)

工作流

  1. 收集需求:问用户哪些测试文件 as 出了问题?是不是大对象只用几条字段?是不是要测错误路径?
  2. 安装迁移:
    • npm i @total-typescript/shoehorn
    • grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts" 找断言点
    • as Type → fromPartial()、as unknown as Type → fromAny()
    • 补 import
    • 跑 type check 验证

适合谁

  • 用 TypeScript 写测试、被 as 拖累的工程师
  • 在 monorepo 里清理测试代码异味的 Tech Lead
  • 教学/分享 mattpocock 测试套路的人

不适合

  • 项目不在 TS 上——本 skill 与 @total-typescript/shoehorn 绑定
  • 生产代码里塞 as——本 skill 明令仅限测试

配套

tdd / test-driven-development(写测试母法)、improve-codebase-architecture(结构性改 testability)、request-refactor-plan(大批量重构 plan 出口)。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@mattpocock · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
macOS · Linux · Windows
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。