如何为从库导入的React组件添加定义

时间:2016-09-10 01:03:45

标签: reactjs material-ui flowtype

我在我的React项目中使用material-ui。我正在尝试将Flow引入混音。

一个例子:

import Paper from 'material-ui/Paper';

type Props = {
  ruleName: string,
  rules: Object[],
  actions: string[],
  allowSubmit: boolean,
  onSubmit: ?Function,
}

const NotifyRule = (props: Props) => (
  <Paper style={{ padding: 10, width: '100%' }}>
      ....
  </Paper>
);

我尝试将material-ui的定义写为

declare module 'material-ui' {
  declare type Paper = any;
  declare var exports: 'material-ui';
}

这不起作用。我失败了:

import Paper from 'material-ui/Paper'; ^^^^^^^^^^^^^^^^^^^ material-ui/Paper. Required module not found

有关解决此问题的任何指示都非常有用。

1 个答案:

答案 0 :(得分:2)

您必须单独声明子模块以使流识别它:

declare module 'material-ui/Paper' {
   declare var exports: any;
}

declare module 'material-ui' {
   // Note: That export value looks weird to me, but whatever
   declare var exports: 'material-ui';
}

我们在flow-typed存储库中使用了redux-saga做了类似的事情......也许你在那里得到了一些灵​​感: - )

https://github.com/flowtype/flow-typed/blob/master/definitions/npm/redux-saga_v0.11.x/flow_%3E%3Dv0.28.x/redux-saga_v0.11.x.js