javascript - React中对象中双管道的目的是什么

标签 javascript reactjs object types

<分区>

我遇到了一些 React DND 示例,其中一个是以下代码:

export type Author = {|
  id: string,
  name: string,
  avatarUrl: string,
  url: string,
|}

有几种类似这样的导出类型,在对象括号中带有双竖线 {|...|},尽管我进行了研究,但我找不到任何可以解释它的作用的内容。我假设这是因为有多个作者被组合到最终对象中(重新:以下代码)并且双管道防止某种冲突。

const princess: Author = {
  id: '4',
  name: 'Princess bubblegum',
  url: '',
  avatarUrl: '',
};

export const authors: Author[] = [
  jake, BMO, finn, princess,
];

最佳答案

这是一个 flowtype 精确的对象类型注释。

https://flow.org/en/docs/types/objects/#toc-exact-object-types

Sometimes it is useful to disable this behavior and only allow a specific set of properties. For this, Flow supports “exact” object types.

基本上,它不会允许定义的 Prop 之外的任何 Prop ,如果你添加,比如...... age: 40Author 对象,它应该会提示。

关于javascript - React中对象中双管道的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48266611/

相关文章:

reactjs - React、Typescript、条件属性

javascript - React Router 链接渲染 #/代替/

reactjs - "Object is possibly undefined"用于可观察字段

javascript - 计算对象的所有属性

python - 将对字符串的引用存储在列表中

javascript - jQuery 滑动切换对应的服务项

javascript - 单击 "Cancel"按钮后的 SweetAlert 调用代码

javascript - Ember : WillTransition does not fire when going directly to URL

PHP/MYSQL错误: Object of class mysqli_result could not be converted to string

javascript - 在 javascript 中管理 secret 信息