extract-react-types icon indicating copy to clipboard operation
extract-react-types copied to clipboard

Props not being extracted when using both memo and forward ref

Open itsdouges opened this issue 3 years ago • 1 comments

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

itsdouges avatar Jul 26 '21 04:07 itsdouges

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)

danieldelcore avatar Jul 26 '21 04:07 danieldelcore