Chuyển đến nội dung
Skillpedia
Kiểm thử310k · #121

migrate-to-shoehorn

Skill này làm gì

Skill chuyển mã kiểm thử dùng các lối tắt về kiểu sang các hàm trợ giúp an toàn hơn của thư viện shoehorn. Dành cho lập trình viên viết kiểm thử TypeScript với dữ liệu kiểm thử một phần.

Khi nào nên dùng

  • ✓Khi các tệp kiểm thử của bạn dùng ép kiểu as để giả dữ liệu.
  • ✓Khi bạn cần dữ liệu kiểm thử một phần mà không phải viết đủ mọi thuộc tính.
  • ✓Khi bạn muốn thay các ép kiểu as unknown as trong kiểm thử.

Liên kết skill

https://github.com/mattpocock/skills/blob/main/skills/misc/migrate-to-shoehorn/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/mattpocock/skills/blob/main/skills/misc/migrate-to-shoehorn/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Thay các ép kiểu as trong tệp kiểm thử của chúng ta bằng shoehorn để các bài kiểm thử vẫn an toàn về kiểu.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent cài thư viện và viết lại từng mẫu kiểm thử thành một hàm trợ giúp tương ứng, như fromPartial cho dữ liệu một phần hoặc fromAny cho dữ liệu cố ý sai. Nó chỉ thay đổi mã kiểm thử.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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 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 } }));

When to use each

Function Use 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
Câu hỏi thường gặp

Tôi có dùng shoehorn trong mã sản xuất được không?

Không. Skill nói nó chỉ dành cho mã kiểm thử.

fromPartial làm gì?

Nó cho phép bạn truyền chỉ các trường mà kiểm thử cần, trong khi TypeScript vẫn kiểm tra kiểu.

fromAny dùng để làm gì?

Nó dành cho các trường hợp kiểm thử cố ý truyền dữ liệu không khớp với kiểu.

Chi tiết

Mô tả gốc của tác giả

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.

repo: mattpocock/skills

Độ phổ biến

Sao GitHub
310k
Vị trí xếp hạng
#121
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự