/* @flow */
type Action = {| type: "A" |}
const action: Action = { type: "A" };
if (action.asdfasdfasdf === "A") {}
action.asdfasdfasdf doesn't exist. Shouldn't it error?
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.
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.