Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Flowtype与react css模块装饰组件一起使用_Javascript_Reactjs_Flowtype_React Css Modules - Fatal编程技术网

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没有产生任何结果

我发现:


最近在GitHub上讨论了这一点。以下是相关问题:

简而言之,问题在于流没有
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