Typescript React Patterns

asyrafhussin/agent-skills/skills/typescript-react-patterns

作者 asyrafhussin1aa0ff717c10MIT86 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫6 週前更新

TypeScript best practices for React development. Use when writing typed React components, hooks, events, refs, or generic components. Triggers on tasks involving TypeScript errors, type definitions, props typing, or type-safe React patterns.

AI 產生的概覽

提供 33 條 TypeScript 規則,為 React 元件、Hook、事件、ref、泛型與 Context 加上型別。

功能
此技能是一份參考指南,包含 7 個類別下的 33 條規則,用於以 TypeScript 撰寫型別安全的 React 程式碼。內容涵蓋元件 props 型別、Hook 型別、事件處理、ref 型別、泛型元件、帶型別的 Context 與狀態,以及工具型別。每條規則位於 rules/ 下的個別檔案中,AGENTS.md 提供完整彙編版本,SKILL.md 中附有簡短範例。
適用情境
在撰寫或修正帶型別的 React 元件、自訂 Hook、事件處理器、ref、泛型元件或帶型別的 Context Provider 時使用。也適合用來解決 React 程式碼中的 TypeScript 錯誤與型別定義問題。
執行需求
不需要指令碼或執行環境相依項目,僅為說明性參考。由代理讀取 Markdown 規則檔案,SKILL.md 中連結了外部 React 與 TypeScript 文件。

TypeScript React Patterns

Type-safe React with TypeScript. Contains 33 rules across 7 categories covering component typing, hooks, event handling, refs, generics, context, and utility types.

Metadata

  • Version: 2.0.0
  • Rule Count: 33 rules across 7 categories
  • License: MIT

When to Apply

Reference these guidelines when:

  • Typing React component props
  • Creating custom hooks with TypeScript
  • Handling events with proper types
  • Working with refs (DOM, mutable, imperative)
  • Building generic, reusable components
  • Setting up typed Context providers
  • Fixing TypeScript errors in React code

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Component TypingCRITICALcomp-
2Hook TypingCRITICALhook-
3Event HandlingHIGHevent-
4Ref TypingHIGHref-
5Generic ComponentsMEDIUMgeneric-
6Context & StateMEDIUMctx-
7Utility TypesLOWutil-

Quick Reference

1. Component Typing (CRITICAL)

  • comp-props-interface - Use interface for props, type for unions
  • comp-children-types - Correct children typing (ReactNode, ReactElement)
  • comp-default-props - Default props with destructuring defaults
  • comp-forward-ref - Typing forwardRef components
  • comp-polymorphic - Polymorphic "as" prop typing
  • comp-fc-vs-function - Function declaration vs React.FC
  • comp-display-name - Display names for debugging
  • comp-rest-props - Spreading rest props with proper types

2. Hook Typing (CRITICAL)

  • hook-usestate - useState with proper generic types
  • hook-useref - useRef for DOM elements and mutable values
  • hook-use-reducer - useReducer with discriminated union actions
  • hook-use-callback - useCallback with typed parameters
  • hook-use-memo - useMemo with typed return values
  • hook-use-context - useContext with null checking
  • hook-custom-hooks - Custom hook return types
  • hook-generic-hooks - Generic custom hooks

3. Event Handling (HIGH)

  • event-handler-types - Event handler type patterns
  • event-click-handler - Click event typing
  • event-form - Form event handling (submit, change, select)
  • event-keyboard - Keyboard event types

4. Ref Typing (HIGH)

  • ref-dom-elements - useRef with specific HTML element types
  • ref-callback - Callback ref pattern for DOM measurement
  • ref-imperative-handle - useImperativeHandle typing

5. Generic Components (MEDIUM)

  • generic-list - Generic list components
  • generic-select - Generic select/dropdown
  • generic-table - Generic table with typed columns
  • generic-constraints - Generic constraints with extends

6. Context & State (MEDIUM)

  • ctx-create - Creating typed context
  • ctx-provider - Provider pattern with null check hook
  • ctx-reducer - Context with useReducer

7. Utility Types (LOW)

  • util-component-props - ComponentPropsWithoutRef for HTML props
  • util-pick-omit - Pick, Omit, Partial for prop derivation
  • util-discriminated-unions - Discriminated unions for state machines

Essential Patterns

Component Props

tsx
interface ButtonProps {  variant: 'primary' | 'secondary' | 'danger'  size?: 'sm' | 'md' | 'lg'  children: React.ReactNode  onClick?: () => void}
function Button({ variant, size = 'md', children, onClick }: ButtonProps) {  return (    <button className={`btn-${variant} btn-${size}`} onClick={onClick}>      {children}    </button>  )}

Typed Context with Null Check

tsx
interface AuthContextType {  user: User | null  login: (credentials: Credentials) => Promise<void>  logout: () => void}
const AuthContext = createContext<AuthContextType | null>(null)
function useAuth() {  const context = useContext(AuthContext)  if (!context) throw new Error('useAuth must be used within AuthProvider')  return context}

Generic Component

tsx
interface ListProps<T> {  items: T[]  renderItem: (item: T) => React.ReactNode  keyExtractor: (item: T) => string}
function List<T>({ items, renderItem, keyExtractor }: ListProps<T>) {  return <ul>{items.map(item => <li key={keyExtractor(item)}>{renderItem(item)}</li>)}</ul>}

How to Use

Read individual rule files for detailed explanations:

rules/comp-props-interface.mdrules/hook-usestate.mdrules/event-form.mdrules/ref-dom-elements.mdrules/util-discriminated-unions.md

References

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

來源與署名

來源:asyrafhussin/agent-skills位於skills/typescript-react-patterns提交1aa0ff7

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架