Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flowtype - making a sealed empty object

Tags:

flowtype

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?

like image 282
Robert Balicki Avatar asked Mar 08 '23 22:03

Robert Balicki


1 Answers

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.

like image 75
Nat Mote Avatar answered Apr 03 '23 22:04

Nat Mote