Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support React.ElementProps #147

Open
rsolomon opened this issue Nov 29, 2017 · 5 comments
Open

Support React.ElementProps #147

rsolomon opened this issue Nov 29, 2017 · 5 comments

Comments

@rsolomon
Copy link

Flow provides a method for obtaining a component's prop types called React.ElementProps (documentation here).

babel-plugin-flow-react-proptypes does not recognize usage, resulting in a transpilation error along these lines:

ERROR in <COMPONENT_PATH>
Module build failed: TypeError: <COMPONENT_PATH>: Cannot read property 'forEach' of undefined
    at convertToPropTypes (/node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:77:19)
    at /node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:31:20
    at Array.forEach (<anonymous>)
    at convertToPropTypes (/node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:27:21)
    at convertToPropTypes (/node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:51:13)
    at /node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:31:20
    at Array.forEach (<anonymous>)
    at convertToPropTypes (/node_modules/babel-plugin-flow-react-proptypes/lib/convertToPropTypes.js:27:21)
    at convertNodeToPropTypes (/node_modules/babel-plugin-flow-react-proptypes/lib/index.js:31:43)
    at PluginPass.TypeAlias (/node_modules/babel-plugin-flow-react-proptypes/lib/index.js:175:25)
@brigand
Copy link
Owner

brigand commented Nov 30, 2017

@rsolomon thanks for the report!

I'm not quite sure what we should do in this case. Could you give an example of how you're using this type in your code?

@rsolomon
Copy link
Author

For example, the material-ui library uses the withStyles HoC for most components. This HoC modifies the exported component's properties. For example:

https://github.com/mui-org/material-ui/blob/v1-beta/src/Menu/MenuItem.js

The component itself takes in Props and ProvidedProps, but the output only requires a diff of Props and DefaultProps. Using React.ElementProps<typeof MenuItem> would get the correctly diffed props, but using the exported Props would mean the user would incorrectly need to provide props that are already provided by either DefaultProps or ProvidedProps.

@brigand
Copy link
Owner

brigand commented Nov 30, 2017

@rsolomon does this look right?

Input:

import MenuItem from 'whatever';

type Props = {
  foo: React.ElementProps<typeof MenuItem>,
};
const C = (props: Props) => {};

Output:

import MenuItem from 'whatever';

const C = (props) => {};

C.propTypes = {
  foo: MenuItem.propTypes
    ? PropTypes.shape(MenuItem.propTypes).isRequired,
    : PropTypes.any.isRequired,
};

Are there other situations I'm missing?

@rsolomon
Copy link
Author

That's correct in that scenario, though usually I've been using the props for compositions. Ex:

import MenuItem from 'whatever';

type Props = React.ElementProps<typeof MenuItem> & {
  foo: string,
};
const C = (props: Props) => {};

With the expected output being:

import MenuItem from 'whatever';

const C = (props) => {};

C.propTypes = {
  ...MenuItem.propTypes,
  foo: PropTypes.string.isRequired,
};

brigand added a commit that referenced this issue Dec 1, 2017
@brigand
Copy link
Owner

brigand commented Dec 1, 2017

@rsolomon I implemented it for this case:

const React = require('react');
const MenuItem = require('./MenuItem');
type Props = {
  foo: React.ElementProps<typeof MenuItem>,
};
const C = (props: Props) => <div />;

Published as 9.2.0. I don't know when I'll have time to implement the intersection case. If you're interested in implementing this, a PR would be great!

If you take it on, you can use ./scripts/runOnFile example.js while developing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants