This is unexpected behaviour:
type A = {| foo?: number, bar?: number |};
({foo: 1, bar: 2}: A); // Both provided: OK
({foo: 1}: A); // One provided: OK
({bar: 1}: A); // One provided: OK
({}: A); /* ???
^ Cannot cast object literal to `A` because inexact object literal [1] is incompatible with exact `A` [2]. */
Can someone confirm this is a bug, or explain why my assumption is wrong here?
See full example on Try.
This is because {} in Flow is an unsealed object which acts differently than what you might expect. To get the behavior you're after, you can use something like {...undefined} or {...null}.
I also just created a ticket to track the discussion around possibly deprecating or removing unsealed objects https://github.com/facebook/flow/issues/7424
Most helpful comment
I also just created a ticket to track the discussion around possibly deprecating or removing unsealed objects https://github.com/facebook/flow/issues/7424