Skip to main content
skill CX Codex

typescript-react-patterns — Claude Code skill: typed React

A skill with 33 TypeScript rules for React: props, hooks, events, refs, generics and context. Useful when the agent writes components and fights type errors.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add AsyrafHussin/agent-skills --skill typescript-react-patterns -a claude-code
Open on GitHub

What typescript-react-patterns does

The rules sit in 7 categories ranked by priority. Component and hook typing come first, then events, refs, generics, context and utility types. Each rule has a prefix (comp-, hook-, event-, ref-, generic-, ctx-, util-) and its own file under rules/, like rules/comp-props-interface.md or rules/hook-usestate.md.

Ready-made examples are included: a button props interface with a variant union, a typed context with a null check, and a generic list component. The agent reaches for these when you ask it to write a component or fix a type error. The full compiled guide lives in AGENTS.md.

Install goes through npx skills add, which drops the skill into your platform's folder — for Claude that is ~/.claude/skills/typescript-react-patterns/SKILL.md. Nothing beyond the skill itself is needed. If your project already has its own typing conventions, they will clash with these 33 rules: the agent pulls in the skill's templates and your code style can drift.

What it helps with

  • type component props without falling back to any
  • fix a TypeScript error inside a hook
  • give an event handler the right type
  • bind a ref to a specific HTML element
  • build a generic list or table component

Example prompts for the agent

  • write a typed Button component with a variant union
  • fix the type error in my custom useAuth hook
  • make a generic List component with renderItem and keyExtractor

How to install typescript-react-patterns

1Skills CLI (Vercel)

npx skills add AsyrafHussin/agent-skills --skill typescript-react-patterns -a claude-code

2Manually: put the files in place

~/.claude/skills/typescript-react-patterns/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/typescript-react-patterns/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about typescript-react-patterns

How do I install typescript-react-patterns in Claude?

Run npx skills add AsyrafHussin/agent-skills --skill typescript-react-patterns -a claude-code. The skill lands in ~/.claude/skills/typescript-react-patterns/SKILL.md.

What does typescript-react-patterns do?

It hands the agent 33 TypeScript rules for React: props, hooks, events, refs, generics, context and utility types. The agent applies them while writing components or untangling type errors.

How is it different from generic TypeScript advice?

Rules are split into files under rules/ with category prefixes, and the full guide is compiled into AGENTS.md. The agent reads the one file it needs instead of the whole document.

Source text

SKILL.md · MIT
---
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`

© agent-skills · MIT · AsyrafHussin/agent-skills

Similar skills

Everything for Frontend and UI →

From the same set12

react-vite-best-practices A skill with 23 rules for optimizing React apps built with Vite: build config, code splitting, HMR, assets, env variables and bundle analysis. Useful when an agent writes or reviews components. CX Codex +1 2,900installs laravel-best-practices A skill with 31 Laravel 13 rules covering architecture, Eloquent, controllers and validation. Useful when your agent writes PHP. CX Codex +1 2,300installs laravel-inertia-react A skill that hands your agent ready rules for Inertia pages, useForm handling and shared data across Laravel and React. CX Codex +1 1,900installs clean-code-principles A skill for architecture review and code quality: SOLID, DRY, KISS, design patterns. Useful when refactoring or weighing design decisions. CX Codex +1 1,800installs api-design-patterns A skill with 38 REST API rules covering resources, errors, security, pagination and versioning. Useful when an agent writes or reviews endpoints. CX Codex +1 929installs laravel-testing Guides AI agents in writing Laravel 13 feature and unit tests with Pest 4 or PHPUnit 12, covering model factories and service mocks. CX Codex +1 907installs testing-best-practices A rule set for unit and integration tests and TDD: 34 rules in 7 categories, TypeScript with Jest and Vitest. Useful while writing tests or reviewing someone else's. CX Codex +1 899installs e2e-playwright-testing A skill that guides the agent through writing Playwright E2E tests: locators, auth reuse, forms, assertions and CI config. CX Codex +1 796installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor