gajus / eslint-plugin-flowtype

Flow type linting rules for ESLint.
Other
1.08k stars 153 forks source link

fix: support union type in React react-only props rule #477

Closed mrtnzlml closed 3 years ago

mrtnzlml commented 3 years ago

This change fixes the following (simplified) example from our application:

type Props =
  | {
      +children: React$Node,
      +title: React$Node,
      +withHiddenTitle?: false,
    }
  | {
      +children: React$Node,
      +title?: React$Node,
      +withHiddenTitle: true,
    };

const props: Props = {children:"", withHiddenTitle: true}
props.title = ""; // correct Flow error

Flow correctly throws the following error:

14: props.title = "";
          ^ Cannot assign empty string to `props.title` because property `title` is not writable. [cannot-write]

However, flowtype/require-readonly-react-props Eslint rule throws the following (incorrect) error:

Props must be $ReadOnly

For simplification, all of the objects in the union must be readonly even though it's probably not a strict requirement in Flow. This can be improved later when needed.

mrtnzlml commented 3 years ago

https://flow.org/try/#0C4TwDgpgBACgTgezAZygXgFBSgHygby22IGoBjACwEsAbAEzggDsAuKAJQgEMzgASAHII6EADRFiUEsCrAaENpx78hI8ZOwkA7rIoAJKnRFMAKrPkB+NgDMuNZGIlQAvkTyENUyrQbNF3XkFhR09pcwgrDgCVYPUNbV0DI2YzOQUoYDgAVxDiZwBuDAwyBCZkYCgwRBQ2eCRUNAJvekZWACI20SgdYH1DY1T5Nkyc1yr6gDoZNPQoDsKgA

gajus commented 3 years ago

:tada: This PR is included in version 5.7.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: