migrate-to-shoehorn

作者: mattpocock

Migrate 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.

npx skills add https://github.com/mattpocock/skills --skill migrate-to-shoehorn

Migrate to Shoehorn

Why shoehorn?

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

Test code only. Never use shoehorn in production code.

Problems with as in tests:

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

Install

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 more properties
};

it("gets user by id", () => {
  // Only care about body.id but must fake entire Request
  getUser({
    body: { id: "123" },
    headers: {},
    cookies: {},
    // ...fake all 20 properties
  });
});

After:

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

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

as TypefromPartial()

Before:

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

After:

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

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

as unknown as TypefromAny()

Before:

getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose

After:

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

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

When to use each

FunctionUse case
fromPartial()Pass partial data that still type-checks
fromAny()Pass intentionally wrong data (keeps autocomplete)
fromExact()Force full object (swap with fromPartial later)

Workflow

  1. Gather requirements - ask user:

    • What test files have as assertions causing problems?
    • Are they dealing with large objects where only some properties matter?
    • Do they need to pass intentionally wrong data for error testing?
  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()
    • Add imports from @total-typescript/shoehorn
    • Run type check to verify

來自 mattpocock 的更多技能

improve-codebase-architecture
mattpocock
在程式碼庫中尋找深化機會,依據 CONTEXT.md 中的領域語言與 docs/adr/ 中的決策。適用於使用者想要改善架構、尋找重構機會、整合緊密耦合的模組,或讓程式碼庫更易於測試及便於 AI 導航時使用。
developmentcode-reviewapi
tdd
mattpocock
以紅-綠-重構循環進行測試驅動開發。當使用者想透過TDD建立功能或修復錯誤、提及「紅-綠-重構」、需要整合測試,或要求測試優先開發時使用。
developmenttesting
handoff
mattpocock
將當前對話壓縮成一份交接文件,供其他代理接手處理。
communicationproject-managementdocument
prototype
mattpocock
建立一個可拋棄的原型,在確定設計前先完善構想。在兩個分支之間切換——一個可執行的終端應用程式,用於測試狀態或商業邏輯問題;或從同一路由切換多種截然不同的UI變體。當使用者想要製作原型、驗證資料模型或狀態機、模擬UI、探索設計選項,或說出「把這個做成原型」、「讓我玩玩看」、「試試幾種設計」時使用。
developmentdesigncreative
triage
mattpocock
透過由分類角色驅動的狀態機來分類問題。當使用者想要建立問題、分類問題、審查傳入的錯誤或功能請求、為AFK代理準備問題,或管理工作流程時使用。
developmentproject-managementcommunication
obsidian-vault
mattpocock
在Obsidian筆記庫中搜尋、建立及管理筆記,支援維基連結與索引筆記。適用於使用者想在Obsidian中尋找、建立或整理筆記時。
productivitydocument
edit-article
mattpocock
編輯並改善文章,透過重組章節、提升清晰度與精煉文句。適用於使用者希望編輯、修訂或改善文章草稿時。
documentcreative
writing-great-skills
mattpocock
撰寫與編輯技能的良好參考——讓技能可預測的詞彙與原則。
documentdevelopment