flow icon indicating copy to clipboard operation
flow copied to clipboard

React HOC changes export type if rendered inside the same file

Open davidswinegar opened this issue 9 years ago • 5 comments

Paste the following into three files in the same project:

TestComp.jsx

// @flow

import React from 'react';

import mapProps from './mapProps';

class TestComp extends React.Component {
  props: {
    id: string;
    value: string;
  };

  render() {
    return (
      <div>
        {this.props.id}
        {this.props.value}
      </div>);
  }
}

const Connected = mapProps(
  (props: {value: string}) => {
    return {
      id: '',
      value: props.value,
    };
  }
)(TestComp);

export default Connected;

// Uncomment this, and flow correctly shows an error in TestParentComp. Otherwise, it does not show an error.
// <Connected value="" />

mapProps.js.flow

// @flow

declare function mapProps<
  DefaultProps,
  ConnectorFnOutputProps: {},
  TotalOutputProps: {},
  State,
  InputProps: $Diff<TotalOutputProps, ConnectorFnOutputProps>,
>(
  connectorFn: (props: InputProps) => ConnectorFnOutputProps
): (component: Class<React$Component<DefaultProps, TotalOutputProps, State>>) =>
  Class<React$Component<{}, InputProps, {}>>;

export default mapProps;

ParentComponent.jsx

// @flow

import React from 'react';

import TestComp from './TestComp';

class ParentComponent extends React.Component {
  render() {
    return (
      <div>
        <TestComp
          // value=""
        />;
      </div>);
  }
}

export default ParentComponent;

If you run flow on these files, there is no error when rendering the TestComp in the ParentComponent file, even though the value prop is missing. However, if you uncomment the line that renders that component within the TestComp file (even though that rendering is a no-op), it does correctly show an error inside the ParentComponent file. Therefore, it seems rendering a component inside a file changes its type to be more concrete. I'm not sure why this happens, but it seems related to my typing of the higher order component (mapProps).

davidswinegar avatar Feb 06 '17 19:02 davidswinegar

I've experienced this problem many times. I believe it may be related to flow being unable to infer types for exports. See https://flowtype.org/docs/modules.html#missingrequired-annotations

derek-duncan avatar Feb 10 '17 17:02 derek-duncan

Hmm, this is confusing because we do have some limited inferred type exports: if I export a React component, I don't have to explicitly annotate it. It's confusing because it's not clear in what situations export types are inferred, and it seems dangerous because we are silently allowing incorrect behavior. Also, a part of the value of flow for me is not having to write these type definitions. I wonder if it's possible to make inferred export types optional.

davidswinegar avatar Feb 13 '17 23:02 davidswinegar

Any plans to repair this? I also meet this problem.

szagi3891 avatar Apr 27 '17 03:04 szagi3891

Any workaround?

sibelius avatar Jul 23 '17 11:07 sibelius

The way this component typing works is different and more explicit now in Flow 0.53, so I'm going to close this. Also note there is new docs around how to do HoCs now.

asolove avatar Aug 29 '17 19:08 asolove