question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Property 'current' is missing in type '(ref: ElementLike | null)

See original GitHub issue

Hi, 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

https://github.com/wouterds/ts-react-hook-form

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
stramelcommented, Sep 23, 2019

@wouterds Is there a reason, you are using a prop called forwardRef instead of using the React.forwardRef function?

const Input = React.forwardRef<HTMLInputElement, Props>((props, ref) => {
  return <Container {...props} ref={ref} />;
});

// Usage
<Form.Input
  id="email"
  type="email"
  name="email"
  ref={register({ required: true })}
/>
3reactions
stramelcommented, Sep 23, 2019

Personally, I would go with

import React, {
  forwardRef,
  DetailedHTMLProps,
  InputHTMLAttributes,
  Ref,
} from 'react';
import { Container } from './styles';

type Props = DetailedHTMLProps<
  InputHTMLAttributes<HTMLInputElement>,
  HTMLInputElement
>;

export default forwardRef<HTMLInputElement, Props>(function Input(props, ref) {
  return <Container {...props} ref={ref} />;
});

Which should fix your displayName linting error

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found