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)
1# 迁移到 Shoehorn23## 为什么用 shoehorn?45`shoehorn` 允许你在 tests 中传入 partial data,同时保持 TypeScript 满意。它用 type-safe alternatives 替换 `as` assertions。67**只用于 test code。** 永远不要在 production code 中使用 shoehorn。89Tests 中 `as` 的问题:1011- 经过训练,不去使用它12- 必须手动指定 target type13- 对故意错误的数据需要 double-as(`as unknown as Type`)1415## 安装1617```bash18npm i @total-typescript/shoehorn19```2021## 迁移模式2223### 只需要少数属性的大型 objects2425Before:2627```ts28type Request = {29 body: { id: string };30 headers: Record<string, string>;31 cookies: Record<string, string>;32 // ...20 more properties33};3435it("gets user by id", () => {36 // Only care about body.id but must fake entire Request37 getUser({38 body: { id: "123" },39 headers: {},40 cookies: {},41 // ...fake all 20 properties42 });43});44```4546After:4748```ts49import { fromPartial } from "@total-typescript/shoehorn";5051it("gets user by id", () => {52 getUser(53 fromPartial({54 body: { id: "123" },55 }),56 );57});58```5960### `as Type` → `fromPartial()`6162Before:6364```ts65getUser({ body: { id: "123" } } as Request);66```6768After:6970```ts71import { fromPartial } from "@total-typescript/shoehorn";7273getUser(fromPartial({ body: { id: "123" } }));74```7576### `as unknown as Type` → `fromAny()`7778Before:7980```ts81getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose82```8384After:8586```ts87import { fromAny } from "@total-typescript/shoehorn";8889getUser(fromAny({ body: { id: 123 } }));90```9192## 何时使用哪一个9394| Function | Use case |95…
Excerpt shown under the repository's MIT license.
