How to infer the ref type of a component?
Let's say you have a component that wraps an input:
And another component using Input, and forwarding the ref to it:
Here, we give
HTMLInputElement
as first type param of React.forwardRef. Is there a way to compute this value dynamically? In other words, is there a way to introspect the type of ref of Input
?
https://codesandbox.io/s/keen-swanson-rcxint?file=/src/App.tsxbrmzkw
CodeSandbox
keen-swanson-rcxint - CodeSandbox
keen-swanson-rcxint by brmzkw using react, react-dom, react-scripts
1 Reply
I tried
React.ComponentPropsWithRef<typeof Input>["ref"]
but it doesn't work, I'm not sure it even makes sense.
wooouhh, actually I can
it seems to work to put the type here.