flow icon indicating copy to clipboard operation
flow copied to clipboard

defaultProps in StatelessFunctionalComponent error

Open andrewkislev opened this issue 8 years ago • 5 comments

Getting error by setting defaultProps in stateless component:

/* @flow */
import * as React from 'react';

type TouchableProps = {borderless?: boolean, children?: React.Node};
type TouchableType = React.StatelessFunctionalComponent<TouchableProps>;

const Touchable: TouchableType = (props: TouchableProps) => <div>{props.children}</div>

Touchable.defaultProps = {borderless: false}

flow/try

Expected behavior: no errors.

andrewkislev avatar Jan 21 '18 13:01 andrewkislev

I just across this myself. The definition doesn't include defaultProps:

/**
 * The type of a stateless functional component. In most cases these components
 * are a single function. However, they may have some static properties that we
 * can type check.
 */
declare type React$StatelessFunctionalComponent<Props> = {
  (props: Props, context: any): React$Node,
  displayName?: ?string,
  propTypes?: $Subtype<{[_: $Keys<Props>]: any}>,
  contextTypes?: any
};

Flow docs do not include types at all. Seems like an oversight to me.

Something that can be PRd perhaps?

casr avatar Mar 05 '18 16:03 casr

If you omit TouchableType flow will infer the correct type. You also want to make the borderless field not optional, flow will infer the correct ElementConfig from Props and defaultProps.

ie:

/* @flow */
import * as React from 'react';

type TouchableProps = {borderless: boolean, children?: React.Node};
type TouchableType = React.StatelessFunctionalComponent<TouchableProps>;

const Touchable = (props: TouchableProps) => <div>{props.children}</div>

Touchable.defaultProps = {
  borderless: false
};

<Touchable>no errors</Touchable>;

flow/try

pigoz avatar Mar 06 '18 08:03 pigoz

@pigoz: There are times when you have no other alternative than using StatelessFunctionalComponent or more generally ComponentType, for example when you want to help flow to get the type of a composition of higher-order components.

It would be super nice if we find a solution to this issue.

mgtitimoli avatar Apr 11 '18 01:04 mgtitimoli

BTW, I forgot to mention that there is a workaround, but it's really ugly, have a look:

const defaultProps = <define your defaultProps here>;

type PropsWithDefaults = $Diff<Props, typeof defaultProps> & $Shape<typeof defaultProps>;

type YourComponent = ComponentType<PropsWithDefaults>; 

// $FlowFixMe: we need this to omit the error originally described :(
Object.assign(YourComponent, {defaultProps});

mgtitimoli avatar Apr 11 '18 01:04 mgtitimoli

I've just noticed that combining $Diff with $Shape breaks the whole type!

image

And AnimatedEllipsis is typed as follows:

const AnimatedEllipsis: ComponentType<
  $Diff<Props, typeof defaultProps> & $Shape<typeof defaultProps>
> = ...

So the last object in the intersection should have all its props optional, and as you can see in the image they are all required, this means that $Shape has not been applied at all.

@calebmer are you aware of this issue?

mgtitimoli avatar Apr 11 '18 01:04 mgtitimoli