Infer ComponentProps of Dialog #601
Replies: 4 comments 6 replies
-
Anyone? |
Beta Was this translation helpful? Give feedback.
-
I'm also looking to create custom component from Dialog and extend its props, haven't found a way to do this properly unfortunately. Would be nice to be able to have |
Beta Was this translation helpful? Give feedback.
-
It would be handy if the library also exported the types that each component uses. That being said, for anyone else who comes across this issue, you can declare a utility type such as: // In a utility types file somewhere in your app
import type { ComponentType } from 'react'
export type ExtractProps<T> = T extends ComponentType<infer P> ? P : T
// Usage in your `.tsx` file
import { Dialog } from '@headlessui/react'
type ModalProps = ExtractProps<typeof Dialog>
export function Modal(props: ModalProps) { ... } Here's a visual example with TypeScript providing intellisense for all available props: Hope it helps 👍 |
Beta Was this translation helpful? Give feedback.
-
You can extend it with a simple intersection type, but it won't be an interface. For example: export type ModalProps = ExtractProps<typeof Dialog> & {
foo: 'bar' | 'baz',
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm trying to create a custom Dialog using headlessui Dialog. My custom Dialog should accept the same props as headlessui one. Using typescript, I'm trying to infer the Props type as I usually do, but It's not working as expected.
This is the error I'm getting:
Any idea? Is this a bug or am I doing something wrong? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions