| name | migrate-to-shoehorn |
| description | Convert `as` type assertions in test files to @total-typescript/shoehorn helpers. Use when the user mentions shoehorn, wants to remove `as` from tests, or needs to pass partial data to a typed function under test. |
Migrate to shoehorn
Why shoehorn
shoehorn (published as @total-typescript/shoehorn) lets a test hand a function partial data without the compiler complaining. It swaps casts for helpers that stay type-checked, so the test still fails when the real shape drifts.
Tests only. Keep shoehorn out of production code.
What as costs you in tests:
- It defeats the compiler you are trying to lean on
- You restate the target type by hand at every call site
- Deliberately wrong fixtures need the double cast
as unknown as Type
Install
npm i @total-typescript/shoehorn
Migration patterns
A large object where the test needs a couple of fields
Before:
type Request = {
body: { id: string };
headers: Record<string, string>;
cookies: Record<string, string>;
};
it("gets user by id", () => {
getUser({
body: { id: "123" },
headers: {},
cookies: {},
});
});
After:
import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});
as Type becomes fromPartial()
Before:
getUser({ body: { id: "123" } } as Request);
After:
import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));
as unknown as Type becomes fromAny()
Before:
getUser({ body: { id: 123 } } as unknown as Request);
After:
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));
Which helper to reach for
| Function | Use case |
|---|
fromPartial() | Supply partial data that still type-checks |
fromAny() | Supply deliberately wrong data (autocomplete stays) |
fromExact() | Require the full object (swap for fromPartial later) |
Workflow
-
Scope the change — ask the user:
- Which test files carry the problematic
as assertions?
- Are these large objects where the test reads only a few fields?
- Do any tests intentionally pass malformed data to exercise error paths?
-
Install, then migrate: