Flow: Unexpected throwing of "Cannot cast object literal to `A` because inexact object literal [1] is incompatible with exact `A`"

Created on 11 Jan 2019  路  2Comments  路  Source: facebook/flow

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.

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

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings