2016-07-22 17 views
10

以下縮減的碼被稱爲:怪異`方法不能在可能爲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.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" 
} 
} 
+0

這是完全一樣的http://stackoverflow.com/questions/38479426/dynamic-type-tests-not-working-as-expected – vkurchatkin

+0

@vkurchatkin最初我以爲儘可能多,但事實並非如此。 'assert(true)'沒有辦法修改'tree.children',因此Flow應該允許這樣做。這與鏈接到可能訪問'this'的成員方法被調用的帖子不同。除非Flow認爲'assert'函數的創建方式可能允許它通過'tree'局部變量捕獲閉包,我不知道。 –

回答

4

您的情況被描述爲here

流量不能知道,那assert不會改變tree。 將下列行添加到您的代碼中並運行它 - 您將遇到運行時錯誤,因爲調用時,assert函數將設置tree.childrennull

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

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

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

accessChildren(root); 

是的,這是很奇怪的代碼,但流不知道,你不會寫它。

+0

感謝您的明確和簡潔的答案。這實際上使負載的感覺! –

4

其他人指出正確的解釋。好在這個工程:

// @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爲只讀的類屬性,流應該能夠保存類型檢查原始代碼。

相關問題