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

Erratic error Property cannot be accessed on possibly undefined value #3059

Closed
singhshashi opened this issue Dec 21, 2016 · 4 comments
Closed

Comments

@singhshashi
Copy link
Contributor

For the following code,


type Person = {name: string, email: string};
type bug = {id: string, assignee: ?Person}

var newBug : bug = {id:'bug1', assignee: null}

if (newBug.assignee) {
  console.log(newBug.assignee.name);
  console.log(newBug.assignee.email);  
}

Flow gives an error:


9:   console.log(newBug.assignee.email);  
                                 ^ property `email`. Property cannot be accessed on possibly null value
9:   console.log(newBug.assignee.email);  
                 ^ null
9:   console.log(newBug.assignee.email);  
                                 ^ property `email`. Property cannot be accessed on possibly undefined value
9:   console.log(newBug.assignee.email);  
                 ^ undefined

If I remove the second console.log, I get no errors. i.e, the following does not give any errors.


type Person = {name: string, email: string};
type bug = {id: string, assignee: ?Person}

var newBug : bug = {id:'bug1', assignee: null}

if (newBug.assignee) {
  console.log(newBug.assignee.name);  
}

Why?

Since both the console.log statements are within the if condition, I don't think the error should come in the first case as well.

Or at least, the behavior should be consistent and same error given in the second case.

The code can be tried at https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoALgTwA4FMwAKuATgM5wB2YAvGAN4UCGAtrgFxinrECWFA5gBowuZox4wOXXgIC+Abgw58AIwCu-GvR4ATKdz5CwjUqR78KudmAD8RMpVmpUAN0bEwlhACENYDuqatHS6bADkgQCMYcImZhZWHBRqMDBOqDxQYAAUXr78AHRx5pa4AJT0qGBgAMaU5DC4BfD8ubg+GkWmJVYFTKxlitV1FA1NLW0dhcUJTaLiMIPVqOlAA

@gcanti
Copy link

gcanti commented Dec 21, 2016

See #2986 (comment)

Fix

const assignee = newBug.assignee
if (assignee) {
  console.log(assignee.name)
  console.log(assignee.email)
}

@singhshashi
Copy link
Contributor Author

Ohk!

@unel
Copy link

unel commented Jan 23, 2017

See #2986 (comment)

I disagree with it. In that example - asynchronous code, but even in async code, some situations can be correctly processed.

In the current example - simple synchronous code.
No one has the ability to change the data between if's predicate and console.log (If we are still talking about JS)

@AugustinLF
Copy link
Contributor

Flow doesn't treat console.log as a different function and considers that it can be unpure. Any function call will unvalidate refinements. There's a discussion somewhere to be able to declare functions as pure, but so far, nothing is settled.

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

No branches or pull requests

4 participants