Property 'current' is missing in type '(ref: ElementLike | null)
See original GitHub issueHi, first of all, this lib looks awesome!
Now, what I’m trying to do is use custom styled components (Form, Input, …) with TypeScript and this lib but I keep running in issues regarding typings of ref. I’ve tried a couple of things now and this seems most correct, however, TS still complains about the return value of register vs the types of the ref prop.
I have this basic form with an email & password input.
const SignIn = () => {
const { register, handleSubmit } = useForm();
const onSubmit = handleSubmit((data: any) => {
console.log({ data });
});
return (
<Layout>
<h1>Sign in</h1>
<Form onSubmit={onSubmit}>
<Form.Field>
<Form.Label htmlFor="email">Email</Form.Label>
<Form.Input
id="email"
type="email"
name="email"
forwardRef={register({ required: true })}
/>
</Form.Field>
<Form.Field>
<Form.Label htmlFor="password">Password</Form.Label>
<Form.Input
id="password"
type="password"
name="password"
forwardRef={register({ required: true })}
/>
</Form.Field>
<Form.Field>
<Form.Button type="submit">Sign in</Form.Button>
</Form.Field>
</Form>
</Layout>
);
};
This is my custom styled Input component
const Container = styled.input`
background: #F00;
`;
interface Props
extends DetailedHTMLProps<
InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
> {
forwardRef?: RefObject<HTMLInputElement>;
}
const Input = (props: Props) => {
return <Container {...props} ref={props.forwardRef} />;
};
But I keep getting Property 'current' is missing in type '(ref: ElementLike | null) => void' but required in type 'RefObject<HTMLInputElement>'.
Example repository
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
javascript - Property 'current' does not exist on type '((instance ...
In typescript, these have the type RefObject<T> , where T is whatever value(s) will be on current . Ref callbacks are another option, ......
Read more >current does not exist on type ref - You.com | The AI Search ...
useRef Typescript error: Property 'current' does not exist on type ... But I keep getting Property 'current' is missing in type ' (ref:...
Read more >Property does not exist on type 'never' in React | bobbyhadz
The error "Property does not exist on type 'never'" occurs when we forget to type a state array or don't type the return...
Read more >Forwarding React Refs with TypeScript | Building SPAs
In the last post, we covered how to use a strongly-typed ref to invoke a method on an HTML element within a React...
Read more >How to Use The Forwarded Ref in React | by Travis Waith-Mair
If it is null, we do nothing. If it is a function, we call the function by passing innerRef.current to the function. And...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@wouterds Is there a reason, you are using a prop called
forwardRefinstead of using theReact.forwardReffunction?Personally, I would go with
Which should fix your displayName linting error