Chuyển đến nội dung
Skillpedia
Lập trình chung15k · #11002

typescript-best-practices

Skill này làm gì

Skill đưa ra các quy tắc viết TypeScript đáng tin cậy. Dành cho lập trình viên muốn ít lỗi kiểu hơn và ít sự cố ẩn hơn.

Khi nào nên dùng

  • ✓Khi bạn yêu cầu agent viết hoặc sửa file TypeScript hay TSX.
  • ✓Khi bạn muốn loại bỏ các ép kiểu không an toàn hoặc kiểu lỏng lẻo khỏi code.
  • ✓Khi bạn muốn kiểm tra dữ liệu từ bên ngoài trước khi code dựa vào nó.

Liên kết skill

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/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/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/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

“Hãy dọn các kiểu trong file TypeScript này để nó không phụ thuộc vào các ép kiểu không an toàn.”

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

Cách hoạt động

Agent làm theo các quy tắc, chẳng hạn dùng biến thể có nhãn, dùng unknown cho dữ liệu bên ngoài, và kiểm tra dữ liệu bằng schema trước khi tin vào kiểu của nó. Các quy tắc giúp trạng thái không thể xảy ra không lọt vào code.

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.

TypeScript best practices

Apply the type-system-discipline principle skill first.

Rule Summary
Discriminated unions Model variants with a kind literal discriminant so impossible states can't be represented. No optional-field bags.
Branded types Brand primitives with & { readonly __brand: "X" } so they can't be mixed up. Validate once at the boundary. A brand comes from a verifying type guard (input is AgentId), never from as.
Constructive modeling Build the shape so the illegal value can't be constructed. [T, ...T[]] for non-empty, [T, T][] for even length, start plus duration for a range. Not a runtime guard, not a wish for refinement types.
Simplest total type Keep T[] while every operation on it stays total. Strengthen to NonEmpty<T> only where the loose type forces !, a cast, or a "should never happen" throw.
unknown over any External data is unknown.
Schemas before guards Before hand-writing a property-by-property type guard, use the repository's runtime schema library and infer the type from the schema, such as z.infer.
No as casts Every as is a runtime crash waiting. Cast only after validation.
Narrowing hierarchy Discriminant switch > in operator > typeof/instanceof > user-defined type guard > as.
Type guards Must verify the claim. A lying guard is worse than as because the bug hides behind a name that says it's safe. Name them isX or hasX.
Exhaustiveness Inline const _exhaustive: never = x; in default arms so the compiler errors when a new variant is added.
satisfies over as Validates the value without widening literal types.
Boundary validation Parse where data crosses in, into a named domain type. Record<string, unknown> (however spelled) stops at that parse. Trust types inside. See the boundary-discipline principle skill.
Schema-derived types Reach for Pick/Omit/Parameters/ReturnType/Awaited/typeof before declaring a new interface.
Object args Pass objects, not positional, so argument order is self-documenting. Skip on hot paths (per-frame render, tokenizers, parsers).
Real tests Don't mock what you can run. Prefer the framework's real test primitives with leak/disposable checks, and verify UI in a running build. Mock only what you can't run locally.
Structured telemetry Prefer structured logger diagnostics with enough context to debug from an id. No console.log in shipped code.

Examples: references/patterns.md.

Câu hỏi thường gặp

Nó có áp dụng cho file JavaScript thuần không?

Các quy tắc dành cho file TypeScript, tức là .ts và .tsx.

Kiểu có thương hiệu là gì?

Một kiểu đánh dấu một giá trị, chẳng hạn một ID, để nó không bị nhầm với giá trị khác.

Vì sao tránh ép kiểu với as?

Ép kiểu bảo trình biên dịch tin giá trị mà không kiểm tra, nên code có thể lỗi khi chạy.

Chi tiết

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

TypeScript best practices. Use when reading or editing any .ts or .tsx file.

repo: cursor/plugins

Độ phổ biến

Sao GitHub
15k
Vị trí xếp hạng
#11002
Giấy phép
—
Cập nhật
31/10/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ự