facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.09k stars 1.85k forks source link

Alternative defaultProps not working with component syntax #9147

Closed neutraali closed 1 month ago

neutraali commented 1 month ago

Flow version: 0.235.1

Expected behavior

defaultProps works when declaring it as:

type Props = {
    name: string
}
component Test(...props: Props) {
   return <h1>{`Hello ${props.name}!`}</h1>;
}
component.defaultProps = {
   name: 'World'
}

Actual behavior

Cannot assign object literal to "Test.defaultProps" because propertydefaultPropsis missing in "React.AbstractComponentStatics"


Try-Flow Link

At the end of the docs detaling React class/function components, there's an "alternative" way to declare defaultProps which we have been using so far. However, this doesn't quite seem to work with component syntax, which is kinda weird because one would assume that they largely follow the rules for functional components.

Is this intentional?

SamChou19815 commented 1 month ago

This is intentional. Note that it will stop working for functions as well starting from React 19: https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops