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.mdSao 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
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
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
Bình luận
- …
Thêm skill từ kho mã này
advisor
15kCho agent hỏi ý kiến một mô hình thứ hai mạnh hơn tại các điểm then chốt, như trước quyết định lớn hoặc khi bị kẹt ở lỗi, nếu bạn bật nó.
check-agent-compatibility
15kKiểm tra xem coding agent AI có khởi động, xác minh và ghi tài liệu được cho một repository không, rồi trả về một điểm tương thích và vấn đề chính.
cli-for-agents
15kDạy agent thiết kế hoặc rà soát công cụ dòng lệnh để các coding agent AI chạy được mà không cần lời nhắc, với trợ giúp rõ ràng và ví dụ.
continual-learning
15kGiữ các ghi chú AGENTS.md của bạn cập nhật bằng cách giao việc rà soát các cuộc trò chuyện trước cho một trình cập nhật bộ nhớ chuyên dụng.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.