Flow's docs say: When you create an object without any properties, you create an unsealed object type in Flow.
Is it possible to create a sealed object without properties?
My use case is as follows. I want to initialize state
to an empty object, and give state the following type:
type EmployeesViewState = {|
employeesRequest?: Request<Array<Employee>>,
geosRequest?: Request<Array<Geo>>,
|};
The error I get when I try to assign an empty object is
33: state: EmployeesViewState = {};
^^ object literal. Inexact type is incompatible with exact type
33: state: EmployeesViewState = {};
^^^^^^^^^^^^^^^^^^ exact type: object type
Of course, since I don't have the requests available yet, I cannot assign them. I also can't assign undefined
to the object because the state is defined with pipes, i.e. it is an exact type.
I can trick flow by saying const a: any = {}; state = a;
but that seems really hacky. Any other ways of tackling this issue?
This is certainly one of Flow's warts. However, it has a fairly simple workaround. Just pass your object through Object.freeze
:
Object.freeze({})
Of course this only works if you weren't going to mutate it, but I find that in cases like this I rarely want to anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With