mcpField
T
typescript-react-patterns

typescript-react-patterns

github.com/asyrafhussinDevelopment951.1k installsUpdated 9w ago
Repository

About typescript-react-patterns

A reference skill for developers writing type-safe React with TypeScript. It organizes 33 rules into seven areas, from component and hook typing to utility types.

Key features

  1. 0133 rules across seven categories
  2. 02Component and hook typing
  3. 03Event and ref typing
  4. 04Generic components and typed context
  5. 05Utility types and state unions

Use cases

  1. 01Typing React component props
  2. 02Building custom hooks
  3. 03Handling typed events and refs
  4. 04Creating reusable generic components

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/asyrafhussin/agent-skills --skill typescript-react-patterns

After installing, type/typescript-react-patternsin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

AGENTS.md
metadata.json
README.md
rules/
_sections.md
_template.md
comp-children-types.md
comp-default-props.md
comp-display-name.md
comp-fc-vs-function.md
comp-forward-ref.md
comp-polymorphic.md
comp-props-interface.md
comp-rest-props.md
ctx-create.md
ctx-provider.md
ctx-reducer.md
event-click-handler.md
event-form.md
event-handler-types.md
event-keyboard.md
generic-constraints.md
generic-list.md
generic-select.md
generic-table.md
hook-custom-hooks.md
hook-generic-hooks.md
hook-use-callback.md
hook-use-context.md
hook-use-memo.md
hook-use-reducer.md

Details

From the project README

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
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
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
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.md
rules/hook-usestate.md
rules/event-form.md
rules/ref-dom-elements.md
rules/util-discriminated-unions.md

References

Full Compiled Document

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

Comments

Markdown supported