Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with: Property cannot be accessed on possibly null value #52

Closed
dugokontov opened this issue Nov 19, 2014 · 8 comments
Closed

Issue with: Property cannot be accessed on possibly null value #52

dugokontov opened this issue Nov 19, 2014 · 8 comments

Comments

@dugokontov
Copy link

I have modified your example 03_Null/nulls.js:

/* @flow */
var x = null;
function length() {
    if (x === null) {
        return 0;
    }
    return x.length;
}

var total = length("Hello") + length();

When I run flow on this file I get following error:

nulls.js:7:16,23: property length
Property cannot be accessed on possibly null value
nulls.js:2:13,16: null

But, if I move line var x = null; inside function length, everything will work as expected. It looks like flow doesn't work well with variables outside it's scope.

@vjeux
Copy link
Contributor

vjeux commented Nov 19, 2014

function length() { ... }
length("Hello")

Flow should warn that you're calling a function that takes 0 arguments with 1 argument no?

@dugokontov
Copy link
Author

It is a valid point, but issue still exist with updated example:

/* @flow */
var x = null;
function length() {
    if (x === null || x === undefined) {
        return 0;
    }
    return x.length;
}

var total = length();

@avikchaudhuri
Copy link
Contributor

Please use x == null as a workaround. (Known issue about Flow not understanding === undefined.)

@avikchaudhuri
Copy link
Contributor

Merging with #21

@dugokontov
Copy link
Author

It looks like this is something else, since this doesn't work even with with x == null

/* @flow */
var x = null;
function length() {
    if (x == null) {
        return 0;
    }
    return x.length;
}

var total = length();

I still get

nulls.js:7:16,23: property length
Property cannot be accessed on possibly null value
nulls.js:2:13,16: null

As I said before, I don't get this error when var x = null; is inside function length

@arb
Copy link

arb commented Aug 26, 2016

@dugokontov did you ever find a solution to this? I'm noticing this issue now with early returns and nullable input arguments.

@dugokontov
Copy link
Author

@arb no ):
For some reason @avikchaudhuri closed this issue a long time ago...

@linuxonrails
Copy link

I have the same problem :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants