-
I have created custom components library on top of chakra-ui. E.g. <Menu>
<Menu.Item>item 1</Menu.Item>
<Menu.Item>item 2</Menu.Item>
<Menu.Item>item 3</Menu.Item>
</Menu> I am able to make it work, my only issue come from typescript. import { Box, forwardRef } from '@chakra-ui/react'
import Item from './Item'
type Props = {
menuType: 'primary' | 'secondary'
}
export const Menu = forwardRef<Props, 'div'>(({ children, ...restProps }, ref) => {
// ... do my stuff
return (
<Box>{children}</Box>
)
})
Menu.Item = Item // Here is the TS error The issue come from forwardRef return types, I got the following ts error: Do you have any resources/best practices about how I should type this kind of component ? |
Beta Was this translation helpful? Give feedback.
Answered by
gamegee
Jan 25, 2021
Replies: 1 comment 3 replies
-
I found a workaround, by forcing the type. I can go with this for now, it's clean import { Box, ComponentWithAs, forwardRef } from '@chakra-ui/react'
import Item from './Item'
type Props = {
menuType: 'primary' | 'secondary'
}
// defined type using chakra ComponentWithAs
type MenuCompoundType = ComponentWithAs<'div', Props> & {
Item?: typeof Item
}
export const Menu: MenuCompoundType = forwardRef(({ children, ...restProps }, ref) => {
// ... do my stuff
return (
<Box>{children}</Box>
)
})
Menu.Item = Item // Works now ! |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
dodas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found a workaround, by forcing the type. I can go with this for now, it's clean