Migrate to shoehorn
テストファイルを `as` 型アサーションから @total-typescript/shoehorn へ移行します。ユーザーが shoehorn に言及した、テスト内の `as` を置き換えたい、部分的なテストデータが必要な場合に使用します。Use when user mentions shoehorn, wants to replace `as` in tests, or needs partial test data. トリガー: shoehorn, as アサーション置き換え, 部分テストデータ, fromPartial, fromAny.From its SKILL.md
npx -y skills add mizunomi32/skills --skill migrate-to-shoehornAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.5 KB, as published. Nobody here has run it
Shoehorn への移行
なぜ shoehorn か
shoehorn を使うと、TypeScript の型を満たしたままテスト内で部分的なデータを渡せます。as アサーションを型安全な代替手段に置き換えます。
テストコード専用です。 本番コードでは shoehorn を絶対に使わないでください。
テスト内で as を使う問題点:
- 使わないよう習慣づけられている
- ターゲット型を手動で指定しなければならない
- 意図的に不正なデータを渡すには二重の
as(as unknown as Type) が必要
インストール
npm i @total-typescript/shoehorn
移行パターン
必要なプロパティが少ない大きなオブジェクト
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 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); // wrong type on purpose
After:
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));
どれを使うか
| 関数 | ユースケース |
|---|---|
fromPartial() | 型チェックを通る部分データを渡す |
fromAny() | 意図的に不正なデータを渡す (自動補完は維持) |
fromExact() | 完全なオブジェクトを強制する (後で fromPartial に置換) |
ワークフロー
-
要件のヒアリング - ユーザーに確認します:
- 問題を引き起こす
asアサーションがあるのはどのテストファイルか? - 一部のプロパティだけが重要な大きなオブジェクトを扱っているか?
- エラーテスト用に意図的に不正なデータを渡す必要があるか?
- 問題を引き起こす
-
インストールと移行:
- インストール:
npm i @total-typescript/shoehorn -
asアサーションを含むテストファイルを探す:grep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts" -
as TypeをfromPartial()に置き換える -
as unknown as TypeをfromAny()に置き換える -
@total-typescript/shoehornから import を追加する - 型チェックを実行して検証する
- インストール: