Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between sealed/ covariant object and $Exact + $ReadOnly?

Is there any difference between:

type MovieType = {|
  +blob?: string,
  +name: string,
  +url?: string
|};

and

type MovieType = $Exact<$ReadOnly<{
  blob?: string,
  name: string,
  url?: string
}>>;

?

I am wondering if flowtype is treating objects differently depending on how they are defined or if the former is just a syntactic sugar for the latter.

like image 572
Gajus Avatar asked Nov 08 '22 06:11

Gajus


1 Answers

Those two object types should be equivalent.

$ReadOnly<T> makes all the properties covariant:

$ReadOnly is a type that represents the read-only version of a given object type T. A read-only object type is an object type whose keys are all read-only.

This means that the following 2 types are equivalent:

type ReadOnlyObj = {
  +key: any,  // read-only field, marked by the `+` annotation
};

type ReadOnlyObj = $ReadOnly<{
  key: any,
}>;

$Exact<T> takes an inexact object and makes it exact:

$Exact<{name: string}> is a synonym for {| name: string |} as in the Object documentation.

like image 98
James Kraus Avatar answered Nov 14 '22 23:11

James Kraus