atlassian / extract-react-types

One stop shop for documenting your react components.
https://atlassian.github.io/extract-react-types/
MIT License
179 stars 28 forks source link

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

Open itsdouges opened 3 years ago

itsdouges commented 3 years ago

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
danieldelcore commented 3 years ago

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)