奇怪的`方法不能在可能的null / undefined值`上调用

时间:2016-07-22 16:19:12

标签: flowtype

以下缩小的代码:

    // @流     'use strict';

import assert from 'assert';

class Node<V, E> {
    value: V;
    children: ?Map<E, Node<V,E>>;

    constructor(value: V) {
        this.value = value;
        this.children = null;
    }
}


function accessChildren(tree: Node<number, string>): void {

    if (tree.children!=null) {
        assert(true); // if you comment this line Flow is ok
        tree.children.forEach( (v,k)=>{});
    } else {
    }

}

...使用以下消息进行流类型检查失败:

$ npm run flow

> simple-babel-serverside-node-only-archetype@1.0.0 flow /home/blah/blah/blah
> flow; test $? -eq 0 -o $? -eq 2

es6/foo.js:21
21:             tree.children.forEach( (v,k)=>{});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `forEach`. Method cannot be called on possibly null value
21:             tree.children.forEach( (v,k)=>{});
^^^^^^^^^^^^^ null

es6/foo.js:21
21:             tree.children.forEach( (v,k)=>{});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `forEach`. Method cannot be called on possibly undefined value
21:             tree.children.forEach( (v,k)=>{});
^^^^^^^^^^^^^ undefined


Found 2 errors

如果读取行assert(true)已注释掉,则表示流程已满足!

是什么给出了?

PS:如果有人想知道,我的.flowconfig.babelrcpackage.json文件都是不明白的:

.flowconfig

$ cat .flowconfig
[options]
esproposal.class_static_fields=enable

.babelrc

$ cat .babelrc
{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread", "transform-flow-strip-types", "transform-class-properties"]
}

的package.json

$ cat package.json
{
"name": "simple-babel-serverside-node-only-archetype",
"version": "1.0.0",
"description": "",
"main": [
"index.js"
],
"scripts": {
"build": "babel es6 --out-dir es5 --source-maps",
"build-watch": "babel es6 --out-dir es5 --source-maps --watch",
"start": "node es5/index.js",
"flow": "flow; test $? -eq 0 -o $? -eq 2"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.4",
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.9.0",
"babel-runtime": "^6.6.1",
"flow-bin": "^0.27.0"
},
"dependencies": {
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-polyfill": "^6.7.4",
"source-map-support": "^0.4.0"
}
}

2 个答案:

答案 0 :(得分:4)

您的案例描述为here

Flow无法知道,assert没有更改tree。 将以下行添加到代码中并运行它 - 您将遇到运行时错误,因为assert函数会在调用时将tree.children设置为null

const root = new Node(1);
const child = new Node(2);

root.children = new Map([['child', child]]);

assert = () => root.children = null;

accessChildren(root);

是的,这是非常奇怪的代码,但Flow不知道,你不会写它。

答案 1 :(得分:4)

其他人指出了正确的解释。幸运的是,这有效:

    // @流     &#39;使用严格的&#39;;

import assert from 'assert';

class Node<V, E> {
  value: V;
  children: ?Map<E, Node<V,E>>;

  constructor(value: V) {
    this.value = value;
    this.children = null;
  }
}


function accessChildren(tree: Node<number, string>): void {

  const children = tree.children; // save possibly mutable reference to local
  if (children!=null) {
    assert(true); // if you comment this line Flow is ok
    children.forEach( (v,k)=>{});
  } else {
  }

}

此外,将来Flow将具有只读属性,并且通过将children声明为类中的只读属性,Flow应该能够保留类型检查原始代码。