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

Optional chaining: inconsistent behavior #8106

Closed
mrtnzlml opened this issue Sep 30, 2019 · 2 comments
Closed

Optional chaining: inconsistent behavior #8106

mrtnzlml opened this issue Sep 30, 2019 · 2 comments

Comments

@mrtnzlml
Copy link
Contributor

mrtnzlml commented Sep 30, 2019

Hello! 👋 I found an issue and I don't know how to explain it (potential bug). I know optional chaining is not fully implemented (#6613) but this seems to be different issue (?). Look at the following code:

// @flow strict

const obj: ?{ field?: ?string } = null;

function variant1() {
  if (obj == null || obj.field == null) {
    return null;
  }

  // Doesn't fail, why?
  // See: https://mrtnzlml.com/docs/javascript#dangerous-getters
  const value: string = obj.field;
}

function variant2() {
  if (obj?.field == null) {
    return null;
  }

  // You could fix it with this:
  // const field = obj?.field;
  // if (field == null) {
  //   return null;
  // }

  // Fails, why? How is it different from the first variant?
  const value: string = obj.field;
}

Both variants seem to be doing the same but they behave differently. The first variant fails but I always considered it to be valid because of how getters can be dangerous, see: https://mrtnzlml.com/docs/javascript#dangerous-getters

But I'd expect that the first variant would fail for the same reason but it doesn't. Why is it behave differently and what is the actual explanation here?

Thank you very much for having a look. :)

https://flow.org/try/#0PTAEAEDMBsHsHdQGcAuAnAlgYxQKF1rAHaqiwBGAVgFygD8A3qJBgKbQAmdtdqmRAc1ABfUAF5QRAK7RoAbnyQpRHBmKgAbgENMWoigCMACgCUoBrlCgMkUEYqVxE6bNAAfN2SoA6Fuw5OkjLQZhZWVmisKFJoREGyClbC+FYgoAAisKxIRADkKMxaGNAANKDwABYAnnSWoGkAyqystBUoKAAOSNQgALZoKEQAXtC90N6EvcAcsFhIwJRa2khYmB0oAMQcegKsaLBSSAC0u+17SHWEJAXa0FItyOgYguJelL5snArJuEoqKGo4tpdPoAEymcx1Gx2Bx0D7+QIuEKQ8KgSLRWLxeR1H6pMAATQOoEIMgCLAAHtYCvAMCgKqA6RhunU0ldSH5OK9YfCviywNCjByAmJnMFQnzwuiYnEkYl6mBcfLQAAxIrQJBlSo1UAACQQ1iQVNAHBskD2rH0zH2vQZFVYzAwaFIwIwehQtSsbJuWjuDz4zyEEgcPI43yAA

@TrySound
Copy link
Contributor

This already asked before here #6613

@mrtnzlml
Copy link
Contributor Author

So is it the same issue after all? I am closing this one then. I really wasn't sure it's the same problem. Thanks! :)

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

2 participants