Flow: Exact object type property not checked inside if

Created on 13 Aug 2017  路  4Comments  路  Source: facebook/flow

/* @flow */

type Action = {| type: "A" |}

const action: Action = { type: "A" };

if (action.asdfasdfasdf === "A") {}

action.asdfasdfasdf doesn't exist. Shouldn't it error?

Most helpful comment

Exact types cannot have extra properties, so this is conceptually wrong. In practice the if will always evaluate to false, which is very unexpected. This case is most of the time a programmer error that I would expect the type system to point out.

I don't see how refinement invalidations have anything to do with it. There are no functions calls in the example.

All 4 comments

This is expected (at least for non-exact object types) and allows for type refining: https://flow.org/en/docs/lang/refinements/#refinement-invalidations

You could argue that this should error (or warn) for exact types, but this is safe.

Exact types cannot have extra properties, so this is conceptually wrong. In practice the if will always evaluate to false, which is very unexpected. This case is most of the time a programmer error that I would expect the type system to point out.

I don't see how refinement invalidations have anything to do with it. There are no functions calls in the example.

I was hit by this multiple times.
Typically:

  handleKeyDown(e: SyntheticEvent) {
    if (e.keyCode === 38) {
      e.preventDefault()
    }
  }

No error is reported, in spite of keyCode not being part of SyntheticEvent...
This one is not a nitpicky issue. It's a real shortcoming !
This is exactly the kind of simple, stupid mistake you would expect Flow to detect !

Closing this since the project has too many open issues to be useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

cubika picture cubika  路  3Comments

mjj2000 picture mjj2000  路  3Comments

philikon picture philikon  路  3Comments

tp picture tp  路  3Comments