Loading...
Loading...
将 test files 从 `as` type assertions 迁移到 @total-typescript/shoehorn。Use when user mentions shoehorn, wants to replace `as` in tests, or needs partial test data.
npx skill4agent add vinvcn/mattpocock-skills-zh-cn migrate-to-shoehornshoehornasasas unknown as Typenpm i @total-typescript/shoehorntype 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
});
});import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});as TypefromPartial()getUser({ body: { id: "123" } } as Request);import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));as unknown as TypefromAny()getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purposeimport { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));| Function | Use case |
|---|---|
| 传入仍能 type-check 的 partial data |
| 传入故意错误的数据(保留 autocomplete) |
| 强制 full object(之后可换成 fromPartial) |
asnpm i @total-typescript/shoehornasgrep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts"fromPartial()as TypefromAny()as unknown as Type@total-typescript/shoehorn