Javascript 将Flowtype与react css模块装饰组件一起使用
下面是显示错误消息的简单组件:Javascript 将Flowtype与react css模块装饰组件一起使用,javascript,reactjs,flowtype,react-css-modules,Javascript,Reactjs,Flowtype,React Css Modules,下面是显示错误消息的简单组件: // @flow import styles from 'styles/components/Error'; import React from 'react'; import CSSModules from 'react-css-modules'; type Props = { message: string } const Error = ({ message }: Props) => { return ( <div style
// @flow
import styles from 'styles/components/Error';
import React from 'react';
import CSSModules from 'react-css-modules';
type Props = {
message: string
}
const Error = ({ message }: Props) => {
return (
<div styleName="error">
{message}
</div>
);
};
export default CSSModules(Error, styles);
Flowtype应该警告我,错误
缺少必需的属性消息
,但它没有。如果我没有用react css模块包装我的Error
组件,Flowtype会按预期工作
我想我需要为Flowtype声明一个类型,以便它理解包装的组件,但我的Google fu没有产生任何结果
我发现:
CSSModules
函数的任何类型信息,因此返回类型被推断为any
换言之:
export default Error; // the type of this export is (_: P) => ?React$element<any>
export default CSSModules(Error, styles); // the type of this export is any
这将在将组件包装到CSSModules
中时保留类型信息,并允许流捕获预期错误
export default Error; // the type of this export is (_: P) => ?React$element<any>
export default CSSModules(Error, styles); // the type of this export is any
declare module 'react-css-modules' {
declare type CssOptions = {
allowMultiple?: boolean,
errorWhenNotFound?: boolean,
};
declare type FunctionComponent<P> = (props: P) => ?React$Element<any>;
declare type ClassComponent<D, P, S> = Class<React$Component<D, P, S>>;
declare function exports<D, P, S, C: ClassComponent<D, P, S> | FunctionComponent<P>>(reactClass: C, styles: Object, cssOptions?: CssOptions): C;
}
[libs]
decls/.js