Chakra UI与Framer Motion组件封装中的类型兼容性问题解析
2025-05-03 23:58:26作者:何举烈Damon
问题背景
在使用Chakra UI与Framer Motion结合开发时,开发者经常会遇到将Chakra组件封装为Motion组件的需求。然而,在最新版本的Chakra UI(2.8.2)中,直接使用motion(Flex)这样的方式会导致TypeScript类型错误,这与之前版本的行为有所不同。
错误现象
当尝试将Chakra UI的Flex组件通过Framer Motion的motion函数进行封装时,TypeScript会抛出以下错误:
Argument of type 'ComponentWithAs<"div", FlexProps>' is not assignable to parameter of type 'string | ForwardRefExoticComponent<MergeWithAs<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, any, FlexProps, As>>'.
Property '$$typeof' is missing in type 'ComponentWithAs<"div", FlexProps>' but required in type 'ForwardRefExoticComponent<MergeWithAs<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, any, FlexProps, As>>'.
问题根源
这个问题的本质在于Chakra UI组件和Framer Motion期望的组件类型不匹配。Chakra UI的组件使用了ComponentWithAs类型,而Framer Motion的motion函数期望接收一个ForwardRefExoticComponent类型的组件。
解决方案
类型断言解决方案
通过类型断言可以明确告诉TypeScript组件的实际类型,使其符合Framer Motion的期望:
import {
As,
Flex,
FlexProps,
Image,
ImageProps,
Box,
ChakraProps,
Text,
TextProps,
MergeWithAs
} from "@chakra-ui/react";
import { motion } from "framer-motion";
import {
DetailedHTMLProps,
ForwardRefExoticComponent,
HTMLAttributes,
ImgHTMLAttributes
} from "react";
export const MotionFlex = motion(
Flex as ForwardRefExoticComponent<
MergeWithAs<
DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>,
any,
FlexProps,
As
>
>
);
export const MotionImage = motion(
Image as ForwardRefExoticComponent<
MergeWithAs<
DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>,
any,
ImageProps,
As
>
>
);
export const MotionBox = motion(
Box as ForwardRefExoticComponent<
MergeWithAs<
DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>,
any,
Omit<ChakraProps, never>,
As
>
>
);
export const MotionText = motion(
Text as ForwardRefExoticComponent<
MergeWithAs<
DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>,
any,
TextProps,
As
>
>
);
方案解析
- 类型断言:使用
as关键字将Chakra组件断言为Framer Motion期望的类型 - 类型结构:每个组件的类型都包含:
- 基础的HTML属性类型
- Chakra特定的props类型
- 组件可能被渲染为的As类型
- 通用模式:对于不同的Chakra组件,只需要替换对应的props类型即可
最佳实践建议
- 集中管理Motion组件:建议将所有Motion封装组件集中在一个文件中导出,便于维护
- 按需封装:不需要为所有Chakra组件都创建Motion版本,只封装实际需要动画效果的组件
- 类型安全:虽然使用了类型断言,但这种模式是类型安全的,因为它准确地描述了组件的实际结构
- 版本兼容性:注意Chakra UI和Framer Motion版本升级可能带来的类型变化
总结
Chakra UI和Framer Motion都是优秀的UI库,但在结合使用时可能会遇到类型兼容性问题。通过合理的类型断言可以解决这些问题,同时保持代码的类型安全性。这种解决方案不仅适用于Flex组件,也可以推广到Chakra UI的其他基础组件上。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0212
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0137
JoyAI-EchoJoyAI-Echo,这是一个独立的、仅用于推理的版本,旨在实现分钟级多镜头音视频生成。它采用了经过蒸馏的DMD生成器、配对的跨模态记忆以及故事级别的一致性。其性能的核心在于,一个跨模态视听记忆库能够在长达五分钟的视频中保持角色外观和语音音色的一致性。同时,一个训练后处理流程将基于记忆的强化学习与分布匹配蒸馏相结合,实现了7.5倍的速度提升,显著增强了视觉质量和对齐效果。00
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
468
461
暂无描述
Dockerfile
775
5.07 K
Ascend Extension for PyTorch
Python
756
960
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
872
2.01 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
696
1.4 K
昇腾LLM分布式训练框架
Python
183
230
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.03 K
271
Oohos_react_native
React Native鸿蒙化仓库
C++
361
430