SuperAPI AI Directory

migrate-to-shoehorn

Coding & development

View repository

Updated 10-15 21:08

About this skill

Migrate to Shoehorn

Why use shoehorn?

shoehorn lets you pass partial data in tests while keeping TypeScript types correct. It replaces as assertions with a type-safe alternative.

Test code only. Never use shoehorn in production code.

as Problems in tests:

  • Trained by habit not to use it
  • Must manually specify the target type
  • Double as(as unknown as Type) for passing intentionally wrong data

Installation

npm i @total-typescript/shoehorn

Migration patterns

Large objects where only a few properties are used

Before migration:

type Request = {
  body: { id: string };
  headers: Record<string, string>;
  cookies: Record<string, string>;
  // ...还有 20 个属性
};

it("gets user by id", () => {
  // 只关心 body.id,但必须伪造整个 Request
  getUser({
    body: { id: "123" },
    headers: {},
    cookies: {},
    // ...伪造所有 20 个属性
  });
});

After migration:

import { fromPartial } from "@total-typescript/shoehorn";

it("gets user by id", () => {
  getUser(
    fromPartial({
      body: { id: "123" },
    }),
  );
});

as Type → fromPartial()

Before migration:

getUser({ body: { id: "123" } } as Request);

After migration:

import { fromPartial } from "@total-typescript/shoehorn";

getUser(fromPartial({ body: { id: "123" } }));

as unknown as Type → fromAny()

Before migration:

getUser({ body: { id: 123 } } as unknown as Request); // 故意传错类型

After migration:

import { fromAny } from "@total-typescript/shoehorn";

getUser(fromAny({ body: { id: 123 } }));

When to use each function

Function Use case
fromPartial() Pass partial data while still passing type checking
fromAny() Pass intentionally wrong data (preserving autocomplete)
fromExact() Force passing a complete object (can later be replaced with fromPartial)

Workflow

  1. Gather requirements — Ask the user:

    • Which test files have as assertions causing problems?
    • Do they involve large objects where only some properties matter?
    • Do they need to pass intentionally wrong data for error tests?
  2. Install and migrate:

    • Install:npm i @total-typescript/shoehorn
    • Find test files containing 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()
    • From @total-typescript/shoehorn add import
    • Run type checking to verify