SuperAPI AI Directory

migrate-to-shoehorn

Coding & development

View repository

Updated 10-15 23:44

About this skill

Migrate to Shoehorn

Why use shoehorn?

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

Test code only. Never use shoehorn in product code.

In tests, as problems:

  • Training does not use it
  • Must manually specify the target type
  • Double as (as unknown as Type) for intentionally wrong data

Installation

npm i @total-typescript/shoehorn

Migration patterns

Large objects with few needed properties

Before:

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

it("通过 id 获取用户", () => {
  // 只关心 body.id 但必须伪造整个 Request
  getUser({
    body: { id: "123" },
    headers: {},
    cookies: {},
    // ...伪造所有 20 个属性
  });
});

After:

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

it("通过 id 获取用户", () => {
  getUser(
    fromPartial({
      body: { id: "123" },
    }),
  );
});

as Type → fromPartial()

Before:

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

After:

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

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

as unknown as Type → fromAny()

Before:

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

After:

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

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

When to use each

Function Use case
fromPartial() Pass partial data that is still type-checked
fromAny() Pass intentionally wrong data (keeps autocomplete)
fromExact() Enforce complete objects (swap with fromPartial later)

Workflow

  1. Gather requirements - Ask the user:

    • Which test files have problematic as assertions?
    • Do they deal with 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 with 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