【问题标题】:What is the difference between sealed/ covariant object and $Exact + $ReadOnly?密封/协变对象和 $Exact + $ReadOnly 有什么区别?
【发布时间】:2018-05-31 21:28:06
【问题描述】:

两者有什么区别:

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

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

?

我想知道 是否根据对象的定义方式对对象进行了不同的处理,或者前者是否只是后者的语法糖。

【问题讨论】:

    标签: flowtype javascript flowtype


    【解决方案1】:

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

    $ReadOnly&lt;T&gt; 使所有属性协变:

    $ReadOnly 是一种表示只读版本的类型 给定对象类型 T。只读对象类型是一个对象类型,其 键都是只读的。

    这意味着以下两种类型是等价的:

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

    $Exact&lt;T&gt; 接受一个不精确的对象并使其精确:

    $Exact 是 {| 的同义词名称:字符串 |} 与 Object 文档中的一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-21
      • 2017-11-08
      • 1970-01-01
      • 2021-11-05
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多