Tooluck logoTooluck
SKILL.md

migrate-to-shoehorn

Agent skill fromvinvcn/mattpocock-skills-zh-CN

将测试文件从 `as` 类型断言迁移到 @total-typescript/shoehorn。适用于用户提到 shoehorn、想替换测试中的 `as`,或需要局部测试数据时。

How to install migrate-to-shoehorn

install migrate-to-shoehorn
# install the mattpocock-skills-zh-CN pack with the skills CLI
npx skills add vinvcn/mattpocock-skills-zh-CN

# or add only this skill to Claude Code: copy the folder

skills/misc/migrate-to-shoehorn/

# into

~/.claude/skills/migrate-to-shoehorn/ (all projects)

.claude/skills/migrate-to-shoehorn/ (one project)

skills/misc/migrate-to-shoehorn/SKILL.md

MITFull file
1# 迁移到 Shoehorn
2
3## 为什么用 shoehorn?
4
5`shoehorn` 允许你在 tests 中传入 partial data,同时保持 TypeScript 满意。它用 type-safe alternatives 替换 `as` assertions。
6
7**只用于 test code。** 永远不要在 production code 中使用 shoehorn。
8
9Tests 中 `as` 的问题:
10
11- 经过训练,不去使用它
12- 必须手动指定 target type
13- 对故意错误的数据需要 double-as(`as unknown as Type`)
14
15## 安装
16
17```bash
18npm i @total-typescript/shoehorn
19```
20
21## 迁移模式
22
23### 只需要少数属性的大型 objects
24
25Before:
26
27```ts
28type Request = {
29 body: { id: string };
30 headers: Record<string, string>;
31 cookies: Record<string, string>;
32 // ...20 more properties
33};
34
35it("gets user by id", () => {
36 // Only care about body.id but must fake entire Request
37 getUser({
38 body: { id: "123" },
39 headers: {},
40 cookies: {},
41 // ...fake all 20 properties
42 });
43});
44```
45
46After:
47
48```ts
49import { fromPartial } from "@total-typescript/shoehorn";
50
51it("gets user by id", () => {
52 getUser(
53 fromPartial({
54 body: { id: "123" },
55 }),
56 );
57});
58```
59
60### `as Type` → `fromPartial()`
61
62Before:
63
64```ts
65getUser({ body: { id: "123" } } as Request);
66```
67
68After:
69
70```ts
71import { fromPartial } from "@total-typescript/shoehorn";
72
73getUser(fromPartial({ body: { id: "123" } }));
74```
75
76### `as unknown as Type` → `fromAny()`
77
78Before:
79
80```ts
81getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose
82```
83
84After:
85
86```ts
87import { fromAny } from "@total-typescript/shoehorn";
88
89getUser(fromAny({ body: { id: 123 } }));
90```
91
92## 何时使用哪一个
93
94| Function | Use case |
95…

Excerpt shown under the repository's MIT license.

More skills in mattpocock-skills-zh-CN

Newsletter

Join the Community

Subscribe to our newsletter for the latest news and updates