typescript-react-patterns

$npx mdskill add AsyrafHussin/agent-skills/typescript-react-patterns

Provides TypeScript best practices for React development.

  • Fixes TypeScript errors in typed React components and hooks.
  • Depends on TypeScript compiler and React type definitions.
  • Selects rules based on task category and priority level.
  • Outputs typed code snippets and type-safe patterns.

SKILL.md

.github/skills/typescript-react-patternsView on GitHub ↗
---
name: typescript-react-patterns
description: 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.
license: MIT
metadata:
  author: agent-skills
  version: "2.0.0"
---

# 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

| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Component Typing | CRITICAL | `comp-` |
| 2 | Hook Typing | CRITICAL | `hook-` |
| 3 | Event Handling | HIGH | `event-` |
| 4 | Ref Typing | HIGH | `ref-` |
| 5 | Generic Components | MEDIUM | `generic-` |
| 6 | Context & State | MEDIUM | `ctx-` |
| 7 | Utility Types | LOW | `util-` |

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

## References

- [React TypeScript Cheatsheet](https://react-typescript-cheatsheet.netlify.app)
- [React + TypeScript Guide](https://react.dev/learn/typescript)
- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/)

## Full Compiled Document

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

More from AsyrafHussin/agent-skills

SkillDescription
clean-code-principlesSOLID principles, design patterns, DRY, KISS, and clean code fundamentals. Use when reviewing architecture, checking code quality, refactoring, or discussing design decisions. Triggers on "review architecture", "check code quality", "SOLID principles", "design patterns", or "clean code".
code-slopDetect AI-generated code patterns ("slop") in PHP/Laravel and TypeScript/React source — comment narration, generic naming, premature interfaces, defensive overdose, mock-everything tests, and the absence of human "scars". Use when reviewing AI-assisted PRs, auditing code for taste/quality (not metrics — that's technical-debt), or hardening a code-review checklist. Triggers on "review for AI slop", "find AI patterns", "check code feels human", "audit code-quality taste".
e2e-playwright-testingEnd-to-end testing with Playwright for web applications. Use when writing E2E tests, browser automation, form submission testing, or user flow testing. Triggers on "playwright", "e2e test", "browser test", "end-to-end", "form flow testing", or test files in tests/e2e/.
laravel-ai-sdkLaravel AI SDK for building AI-powered features. Use when creating agents, generating images or audio, working with embeddings, vector search, or testing AI features. Triggers on tasks involving laravel/ai, AI agents, tool-calling, structured output, streaming, embeddings, reranking, or AI faking in tests.
laravel-best-practicesLaravel 13 conventions and best practices. Use when creating controllers, models, migrations, validation, services, or structuring Laravel applications. Triggers on tasks involving Laravel architecture, Eloquent, database, API development, or PHP patterns.
laravel-database-optimizationLaravel database optimization patterns. Use when writing Eloquent queries, creating migrations, configuring caching, debugging slow queries, or optimizing database performance. Triggers on tasks involving N+1 queries, indexing, Redis caching, pagination, or database transactions.
laravel-inertia-reactLaravel + Inertia.js + React integration patterns. Use when building Inertia page components, handling forms with useForm, managing shared data, or implementing persistent layouts. Triggers on tasks involving Inertia.js, page props, form handling, or Laravel React integration.
laravel-mcpLaravel MCP server development. Use when building MCP servers, tools, prompts, or resources for AI client integration. Triggers on tasks involving laravel/mcp, MCP tools, MCP prompts, MCP resources, or AI client protocols.
laravel-owasp-securityOWASP Top 10 security audit and secure coding guidelines for Laravel + React/Inertia.js applications. Use when auditing for vulnerabilities ("run OWASP audit", "security review", "check my app security") or writing secure Laravel code involving auth, payments, file uploads, or API design. Triggers on security-related tasks, payment handling, authentication, or any request to audit a Laravel codebase.
laravel-queuesLaravel 13 queue and job patterns — driver choice, job design (idempotency, ShouldQueue, model serialisation), retry and failure handling, worker scaling, Bus batching and chaining, Horizon when warranted, queue testing. Use when designing async jobs, scheduling background work, configuring Horizon, debugging stuck jobs, or auditing queue health. Triggers on "Laravel queue", "Laravel job", "background job", "Horizon setup", "failed jobs", "Bus batch", "queue worker tuning".