2017-06-21 111 views
0

我想使用flow类型检查在我的应用程序。我安装了它,并试图运行流量:错误命令失败,退出代码2

丝线行进流

为这个项目运行在Web浏览器中细微这是给我的错误。有没有人有任何想法为什么flow越来越失败?

> yarn run flow                                    
yarn run v0.23.3                                    
$ "D:\ReactJS\todo-app\node_modules\.bin\flow"                             
Launching Flow server for D:\ReactJS\todo-app                             
Spawned flow server (pid=13704)                                
Logs will go to C:\Users\williams~1\AppData\Local\Temp\flow\DzCzBReactJSzBtodo-app.log                   
src/App.js:34                                     
         v                                  
34:  this.state = {                                  
35:  open: false,                                  
36:  todos: [],                                   
37:  notetext: ""                                  
38:  };                                     
     ^object literal. This type is incompatible with                          
30: class App extends Component {                                
         ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:39                                     
39:  this.handleChange = this.handleChange.bind(this);                          
       ^^^^^^^^^^^^ property `handleChange`. Covariant property `handleChange` incompatible with contravariant use in         
39:  this.handleChange = this.handleChange.bind(this);                          
     ^^^^^^^^^^^^^^^^^ assignment of property `handleChange`                        

src/App.js:43                                     
43:  this.setState({ open: true });                              
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `setState`                        
43:  this.setState({ open: true });                              
         ^^^^^^^^^^^^^^ property `open` of object literal. Property cannot be assigned on possibly undefined value        
30: class App extends Component {                                
         ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:47                                     
47:  this.setState({ open: false });                              
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `setState`                        
47:  this.setState({ open: false });                              
         ^^^^^^^^^^^^^^^ property `open` of object literal. Property cannot be assigned on possibly undefined value        
30: class App extends Component {                                
         ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:51                                     
51:  let todos = [...this.state.todos];                             
            ^^^^^ property `todos`. Property cannot be accessed on possibly undefined value           
51:  let todos = [...this.state.todos];                             
         ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:54                                     
54:  text: this.state.notetext,                               
          ^^^^^^^^ property `notetext`. Property cannot be accessed on possibly undefined value            
54:  text: this.state.notetext,                               
       ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?            

src/App.js:57                                     
     v--------------------------------------                            
57:  this.setState({ todos: todos },() => {                            
58:  // setState is async, so this is callback                           
59:  });                                     
     -^ call of method `setState`                               
57:  this.setState({ todos: todos },() => {                            
         ^^^^^^^^^^^^^^^^ property `todos` of object literal. Property cannot be assigned on possibly undefined value       
30: class App extends Component {                                
         ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:60                                     
60:  this.props.addTodo(this.state.notetext);                            
             ^^^^^^^^ property `notetext`. Property cannot be accessed on possibly undefined value         
60:  this.props.addTodo(this.state.notetext);                            
          ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?         

src/App.js:98                                     
98:    open={this.state.open}                              
            ^^^^ property `open`. Property cannot be accessed on possibly undefined value            
98:    open={this.state.open}                              
         ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?          

src/App.js:106                                     
106:    defaultValue={this.state.noteVal}                           
              ^^^^^^^ property `noteVal`. Property cannot be accessed on possibly undefined value        
106:    defaultValue={this.state.noteVal}                           
           ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?        


Found 10 errors                                    
error Command failed with exit code 2.                               
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. 

App.js

/* @flow */ 

import React, { Component } from "react"; 
import logo from "./logo.svg"; 
import "./App.css"; 

import AppBar from "material-ui/AppBar"; 
import FloatingActionButton from "material-ui/FloatingActionButton"; 
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"; 
import * as strings from "./Strings"; 
import * as colors from "./Colors"; 
import styles from "./Styles"; 
import ContentAdd from "material-ui/svg-icons/content/add"; 
import Dialog from "material-ui/Dialog"; 
import FlatButton from "material-ui/FlatButton"; 
import * as injectTapEventPlugin from "react-tap-event-plugin"; 
import TextField from "material-ui/TextField"; 
import { List, ListItem } from "material-ui/List"; 
import { connect } from "react-redux"; 
import { addTodo } from "./actions/index"; 
import * as actions from "./actions/index"; 

const AppBarTest =() => 
    <AppBar 
    title={strings.app_name} 
    iconClassNameRight="muidocs-icon-navigation-expand-more" 
    style={{ backgroundColor: colors.blue_color }} 
    />; 

class App extends Component { 
    constructor(props) { 
    injectTapEventPlugin(); 
    super(props); 
    this.state = { 
     open: false, 
     todos: [], 
     notetext: "" 
    }; 
    this.handleChange = this.handleChange.bind(this); 
    } 

    handleOpen =() => { 
    this.setState({ open: true }); 
    }; 

    handleClose =() => { 
    this.setState({ open: false }); 
    }; 

    handleCreateNote =() => { 
    let todos = [...this.state.todos]; 
    todos.push({ 
     id: todos.length, 
     text: this.state.notetext, 
     completed: false 
    }); 
    this.setState({ todos: todos },() => { 
     // setState is async, so this is callback 
    }); 
    this.props.addTodo(this.state.notetext); 
    this.handleClose(); 
    }; 

    handleChange(event) { 
    this.setState({ [event.target.name]: event.target.value }); 
    } 

    _renderTodos() { 
    return this.props.todos.map(event => { 
     return (
     <ListItem 
      primaryText={event.text} 
      key={event.id} 
      style={{ width: "100%", textAlign: "center" }} 
      onTouchTap={this._handleListItemClick.bind(this, event)} 
     /> 
    ); 
    }); 
    } 

    _handleListItemClick(item) { 
    console.log(item); 
    } 

    render() { 
    return (
     <MuiThemeProvider> 
     <div> 
      <AppBarTest /> 
      <FloatingActionButton 
      style={styles.fab} 
      backgroundColor={colors.blue_color} 
      onTouchTap={this.handleOpen} 
      > 
      <ContentAdd /> 
      </FloatingActionButton> 
      <Dialog 
      open={this.state.open} 
      onRequestClose={this.handleClose} 
      title={strings.dialog_create_note_title} 
      > 
      <TextField 
       name="notetext" 
       hintText="Note" 
       style={{ width: "48%", float: "left", height: 48 }} 
       defaultValue={this.state.noteVal} 
       onChange={this.handleChange} 
       onKeyPress={ev => { 
       if (ev.key === "Enter") { 
        this.handleCreateNote(); 
        ev.preventDefault(); 
       } 
       }} 
      /> 

      <div 
       style={{ 
       width: "4%", 
       height: "1", 
       float: "left", 
       visibility: "hidden" 
       }} 
      /> 

      <FlatButton 
       label={strings.create_note} 
       style={{ width: "48%", height: 48, float: "left" }} 
       onTouchTap={this.handleCreateNote} 
      /> 
      </Dialog> 

      <List style={{ margin: 8 }}> 
      {this._renderTodos()} 
      </List> 

     </div> 
     </MuiThemeProvider> 
    ); 
    } 
} 

function mapStateToProps(state) { 
    return { 
    todos: state.todos 
    }; 
} 

export default connect(mapStateToProps, actions)(App); 

的package.json

{ 
    "name": "todo-app", 
    "version": "0.1.0", 
    "private": true, 
    "dependencies": { 
    "react": "^15.5.4", 
    "react-dom": "^15.5.4", 
    "react-redux": "^5.0.5", 
    "react-tap-event-plugin": "^2.0.1", 
    "redux": "^3.6.0" 
    }, 
    "devDependencies": { 
    "babel-cli": "^6.24.1", 
    "babel-plugin-transform-runtime": "^6.23.0", 
    "babel-preset-es2015": "^6.24.1", 
    "babel-preset-flow": "^6.23.0", 
    "babel-preset-react": "^6.24.1", 
    "babel-preset-stage-0": "^6.24.1", 
    "flow-bin": "^0.48.0", 
    "react-scripts": "1.0.7", 
    "redux-devtools": "^3.4.0", 
    "webpack": "^2.6.1" 
    }, 
    "scripts": { 
    "start": "react-scripts start", 
    "build": "react-scripts build", 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject", 
    "babel-version": "babel --version" 
    } 
} 
+0

退房https://stackoverflow.com/help/mcve - 如果你能分解这个问题会更容易为人们回答。例如,你的许多类型错误是相似的 - 也许你可以创建一个展示一个类型错误的最简单的例子,然后把焦点放在这个问题上。 –

+0

你有10个错误需要修复。例如第一个错误你没有定义状态对象的类型。 'this.state = {open:Boolean,todos:Array,notetext:string}' –

+0

@SlimShady它不需要做出反应。我将如何声明和初始化一个变量? 'open:false'这是我在'this.state'中完成的工作 –

回答

1

流程于它的核心是一种系统,该系统将静态类型检查的JavaScript。

在javascript中,你可以做这样的事情

const add = (a,b) => { 
    return a+b; 
    } 
    add(6,9); // 15 
    add("6","9"); // 69 
    add("Slim ","Shady");//Slim Shady 

但是,使用流,你需要定义的函数变量的类型。

const add = (a: number,b: number): number => { 
    return a+b; 
    } 
     add(6,9); // 15 
     add("6","9"); // error 
     add("Slim ","Shady");// error 

其盈利的使用它在大项目,其中有很多人对此贡献,因为它变成无静态类型检查混乱。还有需要大量记录的项目。

惯于真的建议在小项目中使用,除非球队真正需要它。并在每一个js文件不会使用,因为有时静态类型可以是痛苦

相关问题