将测试文件从 `as` 类型断言迁移到 @total-typescript/shoehorn。适用于用户提到 shoehorn、想替换测试中的 `as`,或需要局部测试数据时。
A migration guide for moving test files away from TypeScript as type assertions and toward the @total-typescript/shoehorn library. The documentation is written in Simplified Chinese and triggers when a user mentions shoehorn, wants to replace as in tests, or needs partial test data. shoehorn lets tests pass partial data while keeping the TypeScript compiler satisfied, replacing as casts with type-safe alternatives.
It is explicit that shoehorn is for test code only and should never appear in production. The rationale is that as assertions in tests train developers to ignore type safety, force manual specification of the target type, and require a double cast through unknown for deliberately wrong data. shoehorn addresses each case with dedicated helpers: fromPartial for passing partial data that still type-checks, fromAny for intentionally wrong data while keeping autocomplete, and fromExact for forcing a full object that can later be relaxed to fromPartial.
The guide includes concrete before-and-after examples, such as faking a large Request object when a test only cares about one field, and shows the corresponding imports from the package. Installation is a single npm install of @total-typescript/shoehorn. A short workflow walks through gathering requirements from the user, installing the package, locating as assertions in test and spec files, replacing as Type with fromPartial and as unknown as Type with fromAny, adding the imports, and finally running a type check to verify. Use it to make test data terser and safer without weakening type checking across a TypeScript test suite.
When migrating test files from as assertions to @total-typescript/shoehorn, or when you need to pass partial test data.
No. It is intended for test code only and should never be used in production.
The fromPartial helper, which lets you pass partial data that still type-checks.
The fromAny helper, used for deliberately wrong data while preserving autocomplete.
With npm i @total-typescript/shoehorn.
Quick Setup:
.claude/skills/Repository
vinvcn/mattpocock-skills-zh-cn