The TypeScript error you're encountering stems from the misuse of generic type parameters with Framer Motion's motion function. Let's break down the issue and provide a clear solution.
Error Explanation
Type 'MotionBoxProps' does not satisfy the constraint 'string'.
This error indicates that the second generic type parameter you're providing to the motion function (MotionBoxProps) does not meet the expected constraint, which is a string. This is because the motion function in Framer Motion expects its second generic parameter to represent an element's ref type, which is typically a string (e.g., "div", "span") or a specific element type, not a combination of props.
Your Current Implementation
// components/MotionBox.tsx
import { motion, MotionProps } from 'framer-motion'
import { Box, BoxProps } from '@chakra-ui/react'
// Combine Chakra's BoxProps with Framer Motion's MotionProps
type MotionBoxProps = BoxProps & MotionProps
// Create a Motion-enhanced Box
const MotionBox = motion<BoxProps, MotionBoxProps>(Box)
export default MotionBox
Here, you're attempting to pass two type parameters (BoxProps and MotionBoxProps) to the motion function. However, Framer Motion's motion function typically only requires one generic type parameter, which is the props type of the component you're enhancing.
Correct Usage of motion with TypeScript
To properly create a motion-enhanced component that combines Chakra UI's BoxProps with Framer Motion's MotionProps, you should only provide one generic type parameter to motion, which represents the props of the base component (Box). Framer Motion internally handles the merging of MotionProps with the provided props.
Here's how you can adjust your code:
// components/MotionBox.tsx
import { motion, MotionProps } from 'framer-motion'
import { Box, BoxProps } from '@chakra-ui/react'
// Combine Chakra's BoxProps with Framer Motion's MotionProps
type MotionBoxProps = BoxProps & MotionProps
// Create a Motion-enhanced Box with only one generic type parameter
const MotionBox = motion<BoxProps>(Box)
export default MotionBox
Alternative Approach: Letting TypeScript Infer Types
TypeScript is often capable of inferring the correct types without explicit generic parameters. You can simplify your component creation by omitting the generic type altogether:
// components/MotionBox.tsx
import { motion } from 'framer-motion'
import { Box } from '@chakra-ui/react'
// Create a Motion-enhanced Box without generic type parameters
const MotionBox = motion(Box)
export default MotionBox
This approach allows TypeScript to automatically infer that MotionBox should accept both BoxProps and MotionProps.
Usage Example
Here's how you can use the newly created MotionBox in your components:
import MotionBox from './components/MotionBox'
const Example = () => (
<MotionBox
p={4}
bg="teal.500"
animate={{ scale: 1.2 }}
transition={{ duration: 0.5 }}
>
Animated Box
</MotionBox>
)
export default Example
Summary
- Issue: Supplying two generic type parameters to
motion where only one is expected.
- Solution: Provide only the base component's props as the generic type or let TypeScript infer the types.
- Result: This resolves the TypeScript error and correctly combines
BoxProps with MotionProps.
By adjusting the generic parameters as shown, the TypeScript error should be resolved, and you'll have a MotionBox component that correctly integrates Chakra UI's Box with Framer Motion's animation capabilities.
References:
Note: Always ensure that your dependencies (framer-motion and @chakra-ui/react) are up-to-date to benefit from the latest type definitions and integrations.