migrate-to-shoehorn
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrate to Shoehorn
迁移至Shoehorn
Why shoehorn?
为什么选择Shoehorn?
shoehornasTest code only. Never use shoehorn in production code.
Problems with in tests:
as- Trained not to use it
- Must manually specify target type
- Double-as () for intentionally wrong data
as unknown as Type
shoehornas仅用于测试代码。 切勿在生产代码中使用shoehorn。
测试中使用的问题:
as- 通常被要求避免使用它
- 必须手动指定目标类型
- 对于故意传入的错误数据需要使用双重断言()
as unknown as Type
Install
安装
bash
npm i @total-typescript/shoehornbash
npm i @total-typescript/shoehornMigration patterns
迁移模式
Large objects with few needed properties
仅需少量属性的大型对象
Before:
ts
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:
ts
import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});之前:
ts
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
});
});之后:
ts
import { fromPartial } from "@total-typescript/shoehorn";
it("gets user by id", () => {
getUser(
fromPartial({
body: { id: "123" },
}),
);
});as Type
→ fromPartial()
as TypefromPartial()as Type
替换为 fromPartial()
as TypefromPartial()Before:
ts
getUser({ body: { id: "123" } } as Request);After:
ts
import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));之前:
ts
getUser({ body: { id: "123" } } as Request);之后:
ts
import { fromPartial } from "@total-typescript/shoehorn";
getUser(fromPartial({ body: { id: "123" } }));as unknown as Type
→ fromAny()
as unknown as TypefromAny()as unknown as Type
替换为 fromAny()
as unknown as TypefromAny()Before:
ts
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purposeAfter:
ts
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));之前:
ts
getUser({ body: { id: 123 } } as unknown as Request); // wrong type on purpose之后:
ts
import { fromAny } from "@total-typescript/shoehorn";
getUser(fromAny({ body: { id: 123 } }));When to use each
何时使用各函数
| Function | Use case |
|---|---|
| Pass partial data that still type-checks |
| Pass intentionally wrong data (keeps autocomplete) |
| Force full object (swap with fromPartial later) |
| 函数名 | 使用场景 |
|---|---|
| 传入仍能通过类型校验的部分数据 |
| 传入故意构造的错误数据(仍保留自动补全功能) |
| 强制传入完整对象(后续可替换为fromPartial) |
Workflow
工作流程
-
Gather requirements - ask user:
- What test files have assertions causing problems?
as - Are they dealing with large objects where only some properties matter?
- Do they need to pass intentionally wrong data for error testing?
- What test files have
-
Install and migrate:
- Install:
npm i @total-typescript/shoehorn - Find test files with assertions:
asgrep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts" - Replace with
as TypefromPartial() - Replace with
as unknown as TypefromAny() - Add imports from
@total-typescript/shoehorn - Run type check to verify
- Install:
-
收集需求 - 询问用户:
- 哪些测试文件中的断言引发了问题?
as - 是否在处理仅需部分属性的大型对象?
- 是否需要为错误测试传入故意构造的错误数据?
- 哪些测试文件中的
-
安装并迁移:
- 安装:
npm i @total-typescript/shoehorn - 查找包含断言的测试文件:
asgrep -r " as [A-Z]" --include="*.test.ts" --include="*.spec.ts" - 将替换为
as TypefromPartial() - 将替换为
as unknown as TypefromAny() - 导入中的相关函数
@total-typescript/shoehorn - 运行类型校验以验证结果
- 安装: