Skip to content

Props not being extracted when using both memo and forward ref #201

Open

Description

@itsdouges

Doesn't work:

import { TextfieldProps } from './types';

const Textfield = forwardRef((props: TextfieldProps, ref) => {
                                     ^^^^^^^^^^^^^^^ not extracted
  return <input />;
});

Textfield.displayName = 'Textfield';

export default memo(Textfield);

Doesn't work:

import { TextfieldProps } from './types';

const Textfield = forwardRef<unknown, TextfieldProps>((props: TextfieldProps, ref) => {
                                      ^^^^^^^^^^^^^^^ not extracted
  return <input />;
});

Textfield.displayName = 'Textfield';

export default memo(Textfield);

Works

import { TextfieldProps } from './types';

const Textfield = forwardRef((props: TextfieldProps, ref) => {
  return <input />;
});

Textfield.displayName = 'Textfield';

export default memo<TextfieldProps>(Textfield);
                    ^^^^^^^^^^^^^^^ extracts, but now missing ref prop in the component types

Activity

  1. danieldelcore commented on Jul 26, 2021

    @danieldelcore
    Collaborator

    Yeah that's a bit of a weird one.

    I think case one and two both work without the memo. We'll need to add some additional smarts to get it to work with it though.

    This should work if you wrap a typed forwardRef in an untyped memo. Not ideal, just sharing incase you were blocked 😄

        import React, { forwardRef, memo } from 'react';
        type MyComponentProps = {
          foo: string,
        }
        const MyComponent = memo(forwardRef<HTMLElement, MyComponentProps>((props, ref) => {
          return <span>Foo</span>;
        }));
        export default MyComponent;

    (https://github.com/atlassian/extract-react-types/blob/master/packages/extract-react-types/converters-typescript.test.js#L656)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions