怪异`方法不能在可能为null /未定义value`
问题描述:
以下缩减的码被称为:怪异`方法不能在可能为null /未定义value`
// @flow
'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 {
}
}
&hellip;失败流动式以下消息检查:
$ npm run flow
> [email protected] 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
,.babelrc
和package.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"
}
}
答
您的情况被描述为here。
流量不能知道,那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);
是的,这是很奇怪的代码,但流不知道,你不会写它。
+0
感谢您的明确和简洁的答案。这实际上使负载的感觉! –
答
其他人指出正确的解释。好在这个工程:
// @flow
'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 {
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 {
}
}
而且,在未来的流向将有只读属性,并通过声明children
为只读的类属性,流应该能够保存类型检查原始代码。
这是完全一样的http://*.com/questions/38479426/dynamic-type-tests-not-working-as-expected – vkurchatkin
@vkurchatkin最初我以为尽可能多,但事实并非如此。 'assert(true)'没有办法修改'tree.children',因此Flow应该允许这样做。这与链接到可能访问'this'的成员方法被调用的帖子不同。除非Flow认为'assert'函数的创建方式可能允许它通过'tree'局部变量捕获闭包,我不知道。 –