Skip to content

Optional property error when property is defined with static defaultProps #7524

@rumax

Description

@rumax

Flow version: 0.78.0 / 0.86.0

Expected behavior

Flow understands static defaultProps and optional properties properly

Actual behavior

Flow does not respect default properties defined with static defaultProps

Example:

Assuming I have the properties defined as following:

type Props = {
  /**
   * A Function. Invoked on load error with {nativeEvent: {error}}.
   */
  onError?: (Error) => void,

  /**
   * A Function. Invoked when load completes successfully.
   */
  onLoad?: () => void,

 ....

And have

static defaultProps = {
    onError: () => {},
    onLoad: () => {},
    ...
  };

Trying to use them lead to flow error:

onError: (event: any) => void;
onError(event: any) {
    const { nativeEvent } = event || {};
    this.props.onError(nativeEvent || new Error('unknown error'));
}

screen shot 2019-03-03 at 12 21 03

Workarounds

Workarounds like making the properties required are not acceptable because:

  • It leads to confusion in documentation
  • It leads to ESLINT error

screen shot 2019-03-03 at 12 24 02

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions