Flow version: 0.134.0
In [email protected] the following code worked without errors:
// @flow strict
import React, {type Element, memo} from 'react';
type PropsType = {}
export const Thing = (props: PropsType): Element<'div'> => {
return <div />;
};
export default memo<PropsType>(Thing);
However, in [email protected] this now throws this error:
Error โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ components/mycomponent.js:28:16
Cannot build a typed interface for this module. You should annotate the exports of this module with types. Cannot
determine the type of this call expression. Please provide an annotation, e.g., by adding a type cast around this
expression. [signature-verification-failure]
25โ </div>
26โ );
27โ
28โ export default memo<PropsType>(Thing);
29โ
I understand that this is a result of flow enabling Types-first checking in 0.134.0 but I am surprised at this failure as React's typed are built-in and the return type of memo() should be handled by default. Am I wrong in that assumption? If so -- what is the recommended way to address this? This way of defining React component is pretty standard in the community.
What's stranger is that I can't reproduce this error on "Try-Flow" but can reproduce it consistently on several machines (and my CI server) when upgrading to 0.134.0.
Yes this is a result of types first being enabled by default. If you plan to keep types first on, which you should since it's the standard, you have to type it as React.AbstractComponent.
Which can look something like this
export default (React.memo<Props>(Thing): React.AbstractComponent<Props, mixed>);
Or you can try a bit cleaner named exports
export const Component: React.AbstractComponent<Props, mixed> = React.memo<Props>(() => {
return null
});
That's good enough for me. A little annoying but worth the tradeoff in improved performance. Thanks for the quick answer!
Most helpful comment
Yes this is a result of types first being enabled by default. If you plan to keep types first on, which you should since it's the standard, you have to type it as
React.AbstractComponent.Which can look something like this