关于 typescript-react-patterns
面向使用 TypeScript 编写类型安全 React 代码的开发者,提供参考规则。内容分为七类共 33 条,涵盖组件、Hook、事件、引用及工具类型。
主要功能
- 01七类共 33 条规则
- 02组件与 Hook 类型定义
- 03事件与引用类型定义
- 04泛型组件与类型化 Context
- 05工具类型与状态联合类型
适用场景
- 01定义 React 组件属性类型
- 02构建自定义 Hook
- 03处理类型化事件与引用
- 04创建可复用泛型组件
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ asyrafhussin/ agent-skills --skill typescript-react-patterns安装后在 Claude Code 中输入/typescript-react-patterns确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdmetadata.jsonREADME.mdrules/_sections.md_template.mdcomp-children-types.mdcomp-default-props.mdcomp-display-name.mdcomp-fc-vs-function.mdcomp-forward-ref.mdcomp-polymorphic.mdcomp-props-interface.mdcomp-rest-props.mdctx-create.mdctx-provider.mdctx-reducer.mdevent-click-handler.mdevent-form.mdevent-handler-types.mdevent-keyboard.mdgeneric-constraints.mdgeneric-list.mdgeneric-select.mdgeneric-table.mdhook-custom-hooks.mdhook-generic-hooks.mdhook-use-callback.mdhook-use-context.mdhook-use-memo.mdhook-use-reducer.md详细介绍
TypeScript React 模式
使用 TypeScript 编写类型安全的 React。包含 7 个类别中的 33 条规则,涵盖组件类型、Hook、事件处理、引用、泛型、上下文和工具类型。
元数据
- 版本: 2.0.0
- 规则数: 7 个类别中的 33 条规则
- 许可证: MIT
适用场景
在以下情况参考这些指南:
- 为 React 组件的属性添加类型
- 使用 TypeScript 创建自定义 Hook
- 使用正确的类型处理事件
- 使用引用(DOM、可变值、命令式操作)
- 构建泛型、可复用的组件
- 设置类型化的 Context 提供程序
- 修复 React 代码中的 TypeScript 错误
按优先级划分的规则类别
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 组件类型 | 严重 | comp- |
| 2 | Hook 类型 | 严重 | hook- |
| 3 | 事件处理 | 高 | event- |
| 4 | 引用类型 | 高 | ref- |
| 5 | 泛型组件 | 中 | generic- |
| 6 | Context 与状态 | 中 | ctx- |
| 7 | 工具类型 | 低 | util- |
快速参考
1. 组件类型(严重)
comp-props-interface- 属性使用 interface,联合类型使用 typecomp-children-types- 正确的 children 类型(ReactNode、ReactElement)comp-default-props- 使用解构默认值设置默认属性comp-forward-ref- 为 forwardRef 组件添加类型comp-polymorphic- 多态 "as" 属性的类型comp-fc-vs-function- 函数声明与 React.FCcomp-display-name- 用于调试的显示名称comp-rest-props- 使用正确类型展开剩余属性
2. Hook 类型(严重)
hook-usestate- 为 useState 指定正确的泛型类型hook-useref- 使用 useRef 引用 DOM 元素和可变值hook-use-reducer- 使用可辨识联合类型的 action 编写 useReducerhook-use-callback- 为 useCallback 指定参数类型hook-use-memo- 为 useMemo 指定返回值类型hook-use-context- 为 useContext 添加 null 检查hook-custom-hooks- 自定义 Hook 的返回类型hook-generic-hooks- 泛型自定义 Hook
3. 事件处理(高)
event-handler-types- 事件处理器的类型模式event-click-handler- 点击事件的类型event-form- 表单事件处理(提交、更改、选择)event-keyboard- 键盘事件类型
4. 引用类型(高)
ref-dom-elements- 使用 useRef 引用特定 HTML 元素类型ref-callback- 用于测量 DOM 的回调引用模式ref-imperative-handle- 为 useImperativeHandle 添加类型
5. 泛型组件(中)
generic-list- 泛型列表组件generic-select- 泛型选择器/下拉菜单generic-table- 使用类型化列的泛型表格generic-constraints- 使用 extends 添加泛型约束
6. Context 与状态(中)
ctx-create- 创建类型化 Contextctx-provider- 使用 null 检查 Hook 的 Provider 模式ctx-reducer- 在 Context 中使用 useReducer
7. 工具类型(低)
util-component-props- 使用 ComponentPropsWithoutRef 指定 HTML 属性util-pick-omit- 使用 Pick、Omit、Partial 派生属性类型util-discriminated-unions- 使用可辨识联合类型构建状态机
核心模式
组件属性
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>
)
}
带 null 检查的类型化 Context
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
}
泛型组件
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>
}
使用方法
阅读各条规则文件,了解详细说明:
rules/comp-props-interface.md
rules/hook-usestate.md
rules/event-form.md
rules/ref-dom-elements.md
rules/util-discriminated-unions.md
参考资料
完整编译文档
完整指南(包含所有展开的规则):AGENTS.md
评论
支持 Markdown