question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing support for Flow Unions

See original GitHub issue

react-docgen doesn’t recognize the props if it’s an union type:

type BaseProps = {|
  label: string,
|};

type LinkButtonProps = {|
  ...BaseProps,
  href: string,
  target?: string,
  rel?: string,
|};

type ButtonProps = {|
  ...BaseProps,
  type?: 'button' | 'submit',
|};

type Props = ButtonProps | LinkButtonProps;

class MyButton extends React.Component<Props> {
  render() {
     // conditionally render `<a>` or `<button>` based on props...
  }
}

I don’t want users to pass props that are specific to <a> at the same time as some props that are specific to <button> - I know this kind of pattern of having a single component render to <a> or <button> is “weird”, but some of our legacy components use it…

For now I need to define a single type that includes all the props and mark them all as optional - which is “fine”, I guess…

More info about unions here: https://flow.org/en/docs/types/unions/

tested on v3.0.0-beta10

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Happy-Ferretcommented, Mar 16, 2018

I’d assume OP is talking about missing descriptions.

With

type: PropTypes.oneOf([
			'animal',
			'bacon',
			'beard',
			'bear',
			'cat',
			'food',
			'city',
			'nature',
			'people',

I automatically receive a description containing all possible enum values.

With FlowType and its unions (i e type?: "animal" | "bacon", no such description is generated.

I’m using react styleguidist, btw.

0reactions
neutraalicommented, Mar 27, 2020

Like @Happy-Ferret said, I think the PropTypes.oneOf([ ... ]) -equivalent for Flow would be closest to ideal here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Union Types - JavaScript. Flow
With union types in Flow you can “refine” the value down to a single type. For example, if we have a value with...
Read more >
Flow union type with exact object types gives 'property is ...
Flow union type with exact object types gives 'property is missing ... branch indicates that the properties are missing on one of the...
Read more >
Solved: Union function missing from function list
This is a screen shot of my flow and the error when trying to find union in the functions list. Solved!
Read more >
Union Type missing properties · Issue #5983 · facebook/flow
Resulting in the following flow exception: Cannot get message.labelName because: • all branches are incompatible: • Either property labelName is ...
Read more >
Making union types even more useful - Scala Contributors
Specifically, the flow typing support for Explicit Null, ... Of course, Union Types in general would probably benefit a lot from wider ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found