package.json 依赖项
...
"dependencies": {
"draft-js": "^0.9.1",
"immutable": "^3.8.1",
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"devDependencies": {
"flow-bin": "^0.37.4"
}
我通过运行安装了带有流类型flow-typed install
的类型定义。
这是我从 Draft-js 文档中复制的一些稍微修改过的代码,我正在尝试使用 flow 进行验证。
index.js
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
class MyEditor extends React.Component {
state: {
editorState: EditorState
}
onChange: Function;
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
// gives type error, as expcted
let a :string = 10;
const editorState :EditorState = this.state.editorState;
// should be a type error, editor.getCurrentContent() returns type ContentState
const content :string = editorState.getCurrentContent();
}
render() {
return (
<Editor editorState={this.state.editorState} onChange={this.onChange} />
);
}
}
ReactDOM.render(
<MyEditor />,
document.getElementById('container')
);
.flowconfig
[ignore]
.*\/node_modules\/draft-js\/lib\/.*.js.flow.*
[include]
[libs]
.*\/node_modules\/draft-js\/lib\/.*.js.flow.*
[options]
esproposal.class_static_fields=enable
suppress_type=$FlowIssue
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue
但是,当我运行flow
命令时,调用 Draft-js API 时不会出现任何错误。在此处设置流程以与草稿一起使用的正确方法是什么?