【问题标题】:What is this format in javascript or typescript?javascript或打字稿中的这种格式是什么?
【发布时间】:2022-07-01 10:15:26
【问题描述】:

javascript 或 typescript 中的这种格式是什么? 我找不到任何信息。

  1. 导出类型 XXX = | true (= |)

  2. $ReadOnly(含义)

  3. | ... |}>

export type AttributeType<T, V> =
  | true
  | $ReadOnly<{|
      diff?: (arg1: T, arg2: T) => boolean,
      process?: (arg1: V) => T,
    |}>;

【问题讨论】:

  • 你从哪里得到这个代码 sn-p?该消息来源对 sn-p 及其语法有何看法?您可以使用哪些资源(课本、课堂笔记、教师讲义、在线教程或参考资料等)来帮助您理解它?
  • $ReadOnly 看起来就像 Typescript 中的泛型类型。 | 用于制作联合类型。所以结果看起来像是返回 true 或传递类型的只读。
  • 这个代码文件是facebook/react-native/Libraries/Renderer/shims/ReactNativeTypes.js
  • 所以..是相同的意思吗?

标签: javascript reactjs typescript react-native format


【解决方案1】:
  1. 代码是打字稿。
  2. $ReadOnly 前缀用于将属性设置为只读。只读成员可以在类外访问,但不能更改其值。由于只读成员不能在类外更改,它们要么需要在声明中初始化,要么在类构造函数内初始化。
  3. 该 |部分将其余部分作为联合,因此它只返回 true 或 read-only 。在只读返回中,diff 将被标识为boolean | undefined;并且process 将被标识为type | undefined;由于所有值都有一个未定义的默认值,因此编译器不会抱怨未分配 processdiff

【讨论】:

  • 和这段代码一样吗? ------ 导出类型 AttributeType = Readonly boolean, process?: (arg1: V) => T }>;跨度>
【解决方案2】:

对于 2,此格式适用于 $ReadOnly(flow 中的实用程序类型之一):

给定对象类型 T 的只读版本

您可以在https://flow.org/en/docs/types/utilities/#toc-readonly获取更多信息

对于 3,您可以在精确的对象类型语法 https://flow.org/en/docs/types/objects/#toc-exact-object-types 中找到格式。示例:{| foo: string, bar: number |}

把它们放在一起,你就可以拥有$ReadOnly&lt;{| foo: string, bar: number |}&gt;。此外,我看到了用法:React.AbstractComponent&lt;{||}&gt; in facebook react-native/Libraries/LogBox/LogBoxInspectorContainer.js

【讨论】:

    【解决方案3】:

    我找到了... Facebook 使用“流量”。 (没有打字稿) https://flow.org/en/

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 2017-05-25
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2019-10-24
      • 2017-01-30
      • 2018-07-09
      • 2022-12-14
      相关资源
      最近更新 更多