密封/协变对象与$Exact + $ReadOnly有什么区别?

5

这两者之间有什么不同:

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

并且

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

?

我想知道是否根据对象的定义方式而有所不同,或者前者只是后者的语法糖。
1个回答

1

这两种对象类型应该是等效的。

$ReadOnly<T> 使所有属性协变:

$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>将不精确的对象变为精确的对象:

$Exact<{name: string}>是Object文档中{| name: string |}的同义词。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接