gpt4 book ai didi

javascript - 密封/协变对象和 $Exact + $ReadOnly 之间有什么区别?

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:50 25 4
gpt4 key购买 nike

有什么区别:

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

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

?

我想知道 正在根据对象的定义方式或前者是否只是后者的语法糖来区别对待对象。

最佳答案

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

$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}> is a synonym for {| name: string |} as in the Object documentation.

关于javascript - 密封/协变对象和 $Exact + $ReadOnly 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50624661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com