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

Sealed object properties not checked in conditionals #5477

Closed
kevanstannard opened this issue Dec 6, 2017 · 2 comments
Closed

Sealed object properties not checked in conditionals #5477

kevanstannard opened this issue Dec 6, 2017 · 2 comments

Comments

@kevanstannard
Copy link

kevanstannard commented Dec 6, 2017

Is it correct that sealed object properties are not checked in conditional statements?

For example:

// Create a sealed object
const o = {
  Foo: 'Foo',
};

// Reports an Error
const fooz = o.Fooz;

// Reports an Error
const x = o.Fooz === 'Bar';

// Does not report an error
if (o.Fooz === 'Bar') {}

// Does not report an error
const y = (o.Fooz === 'Bar') ? true : false;

Edit: Added some more examples. It looks like intermediate expressions (the if statement and the ternary) do not have the type check applied.

@popham
Copy link
Contributor

popham commented Dec 10, 2017

Non-existence is not an error within a conditional expression. See the comment from

flow/src/typing/statement.ml

Lines 4453 to 4456 in c9131da

(* Conditional expressions are checked like expressions, except that property
accesses are provisionally allowed even when such properties do not exist.
This accommodates the common JavaScript idiom of testing for the existence
of a property before using that property. *)

@kevanstannard
Copy link
Author

@popham many thanks for clarifying.

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

2 participants