typescript-react-patterns
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
- 最近更新
- 2026-09-01
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- typescript-react-patterns
- license
- MIT
- metadata
- { "author": "agent-skills", "version": "2.0.0" }
- 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.
02 资源文件 37
- 参考文档AGENTS.md149.0 KB
- 参考文档README.md1.7 KB
- 参考文档rules/_sections.md2.0 KB
- 参考文档rules/_template.md2.3 KB
- 参考文档rules/comp-children-types.md3.5 KB
- 参考文档rules/comp-default-props.md5.4 KB
- 参考文档rules/comp-display-name.md4.4 KB
- 参考文档rules/comp-fc-vs-function.md2.7 KB
- 参考文档rules/comp-forward-ref.md5.1 KB
- 参考文档rules/comp-polymorphic.md4.7 KB
- 参考文档rules/comp-props-interface.md3.2 KB
- 参考文档rules/comp-rest-props.md4.9 KB
- 参考文档rules/ctx-create.md5.0 KB
- 参考文档rules/ctx-provider.md3.9 KB
- 参考文档rules/ctx-reducer.md4.9 KB
- 参考文档rules/event-click-handler.md4.5 KB
- 参考文档rules/event-form.md3.7 KB
- 参考文档rules/event-handler-types.md3.9 KB
- 参考文档rules/event-keyboard.md3.6 KB
- 参考文档rules/generic-constraints.md4.0 KB
- 参考文档rules/generic-list.md4.4 KB
- 参考文档rules/generic-select.md3.8 KB
- 参考文档rules/generic-table.md3.7 KB
- 参考文档rules/hook-custom-hooks.md7.2 KB
- 参考文档rules/hook-generic-hooks.md9.4 KB
- 参考文档rules/hook-use-callback.md5.2 KB
- 参考文档rules/hook-use-context.md6.1 KB
- 参考文档rules/hook-use-memo.md6.4 KB
- 参考文档rules/hook-use-reducer.md7.1 KB
- 参考文档rules/hook-useref.md4.3 KB
- 参考文档rules/hook-usestate.md3.9 KB
- 参考文档rules/ref-callback.md3.3 KB
- 参考文档rules/ref-dom-elements.md3.6 KB
- 参考文档rules/ref-imperative-handle.md3.7 KB
- 参考文档rules/util-component-props.md3.8 KB
- 参考文档rules/util-discriminated-unions.md5.0 KB
- 参考文档rules/util-pick-omit.md4.1 KB
03 SKILL.md 正文
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 unionscomp-children-types- Correct children typing (ReactNode, ReactElement)comp-default-props- Default props with destructuring defaultscomp-forward-ref- Typing forwardRef componentscomp-polymorphic- Polymorphic "as" prop typingcomp-fc-vs-function- Function declaration vs React.FCcomp-display-name- Display names for debuggingcomp-rest-props- Spreading rest props with proper types
2. Hook Typing (CRITICAL)
hook-usestate- useState with proper generic typeshook-useref- useRef for DOM elements and mutable valueshook-use-reducer- useReducer with discriminated union actionshook-use-callback- useCallback with typed parametershook-use-memo- useMemo with typed return valueshook-use-context- useContext with null checkinghook-custom-hooks- Custom hook return typeshook-generic-hooks- Generic custom hooks
3. Event Handling (HIGH)
event-handler-types- Event handler type patternsevent-click-handler- Click event typingevent-form- Form event handling (submit, change, select)event-keyboard- Keyboard event types
4. Ref Typing (HIGH)
ref-dom-elements- useRef with specific HTML element typesref-callback- Callback ref pattern for DOM measurementref-imperative-handle- useImperativeHandle typing
5. Generic Components (MEDIUM)
generic-list- Generic list componentsgeneric-select- Generic select/dropdowngeneric-table- Generic table with typed columnsgeneric-constraints- Generic constraints with extends
6. Context & State (MEDIUM)
ctx-create- Creating typed contextctx-provider- Provider pattern with null check hookctx-reducer- Context with useReducer
7. Utility Types (LOW)
util-component-props- ComponentPropsWithoutRef for HTML propsutil-pick-omit- Pick, Omit, Partial for prop derivationutil-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
04 同仓库更多 Skills
wordpress-pro
Develops custom WordPress themes and plugins, creates and registers Gutenberg blocks and block patterns, configures WooCommerce stores, implements WordPress REST API endpoints, applies security hardening (nonces, sanitization, escaping, capability checks), and optimizes performance through caching and query tuning. Use when building WordPress themes, writing plugins, customizing Gutenberg blocks, extending WooCommerce, working with ACF, using the WordPress REST API, applying hooks and filters, or improving WordPress performance and security.
websocket-engineer
Use when building real-time communication systems with WebSockets or Socket.IO. Invoke for bidirectional messaging, horizontal scaling with Redis, presence tracking, room management.
webapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.
web-design-guidelines
UI/UX best practices and accessibility audit. Use when reviewing UI code, checking accessibility, running accessibility audits, auditing forms, or ensuring web interface best practices. Triggers on "audit accessibility", "check WCAG", "review UI", "check accessibility", "audit design", "review UX", or "check best practices".
web-artifacts-builder
Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use for complex artifacts requiring state management, routing, or shadcn/ui components - not for simple single-file HTML/JSX artifacts.
vue-expert
Builds Vue 3 components with Composition API patterns, configures Nuxt 3 SSR/SSG projects, sets up Pinia stores, scaffolds Quasar/Capacitor mobile apps, implements PWA features, and optimises Vite builds. Use when creating Vue 3 applications with Composition API, writing reusable composables, managing state with Pinia, building hybrid mobile apps with Quasar or Capacitor, configuring service workers, or tuning Vite configuration and TypeScript integration.