$npx -y skills add stevesolun/ctx --skill migrate-to-shoehornMigrate 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.
| 1 | # Migrate to Shoehorn |
| 2 | |
| 3 | ## Why shoehorn? |
| 4 | |
| 5 | `shoehorn` lets you pass partial data in tests while keeping TypeScript happy. It replaces `as` assertions with type-safe alternatives. |
| 6 | |
| 7 | **Test code only.** Never use shoehorn in production code. |
| 8 | |
| 9 | Problems with `as` in tests: |
| 10 | |
| 11 | - Trained not to use it |
| 12 | - Must manually specify target type |
| 13 | - Double-as (`as unknown as Type`) for intentionally wrong data |
| 14 | |
| 15 | ## Install |
| 16 | |
| 17 | ```bash |
| 18 | npm i @total-typescript/shoehorn |
| 19 | ``` |
| 20 | |
| 21 | ## Migration patterns |
| 22 | |
| 23 | ### Large objects with few needed properties |
| 24 | |
| 25 | Before: |
| 26 | |
| 27 | ```ts |
| 28 | type Request = { |
| 29 | body: { id: string }; |
| 30 | headers: Record<string, string>; |
| 31 | cookies: Record<string, string>; |
| 32 | // ...20 more properties |
| 33 | }; |
| 34 | |
| 35 | it("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 | |
| 46 | After: |
| 47 | |
| 48 | ```ts |
| 49 | import { fromPartial } from "@total-typescript/shoehorn"; |
| 50 | |
| 51 | it("gets user by id", () => { |
| 52 | getUser( |
| 53 | fromPartial({ |
| 54 | body: { id: "123" }, |
| 55 | }), |
| 56 | ); |
| 57 | }); |
| 58 | ``` |
| 59 | |
| 60 | ### `as Type` → `fromPartial()` |
| 61 | |
| 62 | Before: |
| 63 | |
| 64 | ```ts |
| 65 | getUser({ body: { id: "123" } } as Request); |
| 66 | ``` |
| 67 | |
| 68 | After: |
| 69 | |
| 70 | ```ts |
| 71 | import { fromPartial } from "@total-typescript/shoehorn"; |
| 72 | |
| 73 | getUser(fromPartial({ body: { id: "123" } })); |
| 74 | ``` |
| 75 | |
| 76 | ### `as unknown as Type` → `fromAny()` |
| 77 | |
| 78 | Before: |
| 79 | |
| 80 | ```ts |
| 81 | getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose |
| 82 | ``` |
| 83 | |
| 84 | After: |
| 85 | |
| 86 | ```ts |
| 87 | import { fromAny } from "@total-typescript/shoehorn"; |
| 88 | |
| 89 | getUser(fromAny({ body: { id: 123 } })); |
| 90 | ``` |
| 91 | |
| 92 | ## When to use each |
| 93 | |
| 94 | | Function | Use case | |
| 95 | | --------------- | -------------------------------------------------- | |
| 96 | | `fromPartial()` | Pass partial data that still type-checks | |
| 97 | | `fromAny()` | Pass intentionally wrong data (keeps autocomplete) | |
| 98 | | `fromExact()` | Force full object (swap with fromPartial later) | |
| 99 | |
| 100 | ## Workflow |
| 101 | |
| 102 | 1. **Gather requirements** - ask user: |
| 103 | - What test files have `as` assertions causing problems? |
| 104 | - Are they dealing with large objects where only some properties matter? |
| 105 | - Do they need to pass intentionally wrong data for error testing? |
| 106 | |
| 107 | 2. **Install and migrate**: |
| 108 | - [ ] Install: `npm i @total-typescript/shoehorn` |
| 109 | - [ ] Find test files with `as` assertions: `grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"` |
| 110 | - [ ] Replace `as Type` with `fromPartial()` |
| 111 | - [ ] Replace `as unknown as Type` with `fromAny()` |
| 112 | - [ ] Add imports from `@total-typescript/shoehorn` |
| 113 | - [ ] Run type check to verify |