brigand / brigand/babel-plugin-flow-react-proptypes
PropTypes based on constants
- Dominant language
- JavaScript
- Stars
- 427
- Forks
- 42
- PR merge metrics
- No merged PRs in 30d
Description
Hey folks!
I want to validate if a property is one of a set of types or values:
```js
type Props = {
type: 'NEWS' | 'POSITIVE'
};
```
This works fine and compiles to:
```js
Alert.propTypes = {
type: require("prop-types").oneOf(["NEWS", "POSITIVE"]).isRequired
};
```
________________
However I have a file where I define my constants. So I have something like this:
```
// constants/alertTypes.js
//@flow
export type AlertType = $Keys;
const ALERT_TYPES = {
NEWS: "NEWS",
POSITIVE: "POSITIVE"
};
module.exports = ALERT_TYPES;
```
```js
import { NEWS, POSITIVE } from "./constants/alertTypes";
type Props = {
type: NEWS | POSITIVE
};
```
This compiles to:
```js
Alert.propTypes = {
type: require("prop-types").oneOfType([typeof _alertTypes.NEWS === "function" ? require("prop-types").instanceOf(_alertTypes.NEWS) : require("prop-types").any, typeof _alertTypes.POSITIVE === "function" ? require("prop-types").instanceOf(_alertTypes.POSITIVE) : require("prop-types").any]).isRequired
};
```
In other words it sets `PropTypes.any` :(
Contributor guide
Assessment
This issue has not been assessed yet.