Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird `Method cannot be called on possibly null / undefined value`

Tags:

flowtype

The following narrowed-down code:

// @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 {
    }

}

… fails Flow type checking with the following message:

$ 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

If the line reading: assert(true) is commented out, flow is satisfied !

What gives?

PS: In case anyone wonders, my .flowconfig, .babelrc and package.json files are nondescript:

.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"
}
}
like image 522
Marcus Junius Brutus Avatar asked Jul 22 '16 16:07

Marcus Junius Brutus


2 Answers

Your case is described here.

Flow cannot know, that assert doesn't change the tree. Add the following lines to your code and run it – you will have runtime error, because assert function will set tree.children to null when called.

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

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

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

accessChildren(root);

Yes, it is pretty weird code, but Flow doesn't know, you will not to write it.

like image 106
timofeyka Avatar answered Oct 05 '22 15:10

timofeyka


Others have pointed to the right explanation. Fortunately this works:

// @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 {
  }
}

Also, in the future Flow will have read-only properties and by declaring children as a read-only property in the class, Flow should be able to preserve type check the original code.

like image 30
Avik Chaudhuri Avatar answered Oct 05 '22 13:10

Avik Chaudhuri