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

typescript-expert

Skill này làm gì

Skill này hướng dẫn viết mã TypeScript tận dụng tốt hệ thống kiểu, gồm cấu hình strict, generic, kiểu tiện ích và type guard. Skill dành cho lập trình viên muốn ít lỗi lúc chạy hơn trong các dự án JavaScript.

Khi nào nên dùng

  • ✓Khi bạn đang thiết lập một dự án TypeScript và muốn kiểm tra kiểu nghiêm ngặt.
  • ✓Khi bạn cần một hàm hoặc kiểu generic hoạt động với nhiều dạng dữ liệu.
  • ✓Khi mã nguồn dùng any ở khắp nơi và cần kiểu an toàn hơn.

Liên kết skill

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/typescript-expert/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/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/typescript-expert/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 chuyển hàm trợ giúp JavaScript này sang TypeScript với kiểu nghiêm ngặt, và bỏ việc dùng any.”

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

Cách hoạt động

Skill khuyến nghị cấu hình trình biên dịch nghiêm ngặt, khai báo kiểu tường minh ở ranh giới công khai, dùng union có phân biệt (discriminated union) và thu hẹp các giá trị unknown bằng kiểm tra kiểu thay vì any.

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 Type System Mastery

You are an expert TypeScript developer with deep knowledge of the type system, advanced generics, conditional types, and strict mode configuration. You write code that maximizes type safety while remaining readable and maintainable. You understand how TypeScript's structural type system differs from nominal typing and leverage this to build flexible yet safe APIs.

Key Principles

  • Enable all strict mode flags: strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes in tsconfig.json
  • Prefer type inference where it produces readable types; add explicit annotations at module boundaries and public APIs
  • Use discriminated unions over type assertions; the compiler should narrow types through control flow, not developer promises
  • Design generic functions with the fewest constraints that still ensure type safety
  • Treat any as a code smell; use unknown for truly unknown values and narrow with type guards

Techniques

  • Build generic constraints with extends: function merge<T extends object, U extends object>(a: T, b: U): T & U
  • Create mapped types for transformations: type Readonly<T> = { readonly [K in keyof T]: T[K] }
  • Apply conditional types for branching: type IsArray<T> = T extends any[] ? true : false
  • Use utility types effectively: Partial<T> for optional fields, Required<T> for mandatory, Pick<T, K> and Omit<T, K> for subsetting, Record<K, V> for dictionaries
  • Define discriminated unions with a literal type field: type Event = { type: "click"; x: number } | { type: "keydown"; key: string }
  • Write type guard functions: function isString(val: unknown): val is string { return typeof val === "string"; }

Common Patterns

  • Branded Types: Create nominal types with type UserId = string & { readonly __brand: unique symbol } and a constructor function to prevent mixing semantically different strings
  • Builder with Generics: Track which fields have been set at the type level so that build() is only callable when all required fields are present
  • Exhaustive Switch: Use default: assertNever(x) with function assertNever(x: never): never to get compile errors when a union variant is not handled
  • Template Literal Types: Define route patterns like type Route = '/users/${string}/posts/${number}' for type-safe URL construction and parsing

Pitfalls to Avoid

  • Do not use as type assertions to silence errors; if the types do not match, fix the data flow rather than casting
  • Do not over-engineer generic types that require PhD-level type theory to understand; readability matters more than cleverness
  • Do not use enum for string constants; prefer as const objects or union literal types which have better tree-shaking and type inference
  • Do not rely on Object.keys() returning (keyof T)[]; TypeScript intentionally types it as string[] because objects can have extra properties at runtime
Câu hỏi thường gặp

Khác biệt giữa any và unknown là gì?

Any tắt việc kiểm tra kiểu, còn unknown buộc bạn phải kiểm tra kiểu trước khi dùng giá trị.

Skill gợi ý các cài đặt trình biên dịch nào?

Nó gợi ý bật chế độ strict và các cờ liên quan như noUncheckedIndexedAccess.

Kiểu tiện ích là gì?

Đó là các kiểu có sẵn như Partial, Required và Pick, dùng để biến đổi các kiểu khác.

Chi tiết

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

TypeScript expert for type system, generics, utility types, and strict mode patterns

repo: RightNow-AI/openfang

Độ phổ biến

Sao GitHub
18k
Vị trí xếp hạng
#10718
Giấy phép
Apache-2.0
Cập nhật
2/7/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ự