【问题标题】:Setting types in Components?在组件中设置类型?
【发布时间】:2019-11-18 13:54:19
【问题描述】:

有人能解释一下这个语法是做什么的吗?我没看过很多

type ScrollProps = {
  location: Object,
  elementID?: string
};

class ScrollMemory extends Component<ScrollProps> {
  detectPop: () => void;
  url: Map<string, number>;

原文出处

https://github.com/ipatate/react-router-scroll-memory/blob/master/src/ScrollMemory.js

【问题讨论】:

  • 那里有 7 行,你的问题到底是什么?
  • 这是打字稿
  • Flow 和 Typescript 都使用type 语法,但它们链接的示例实际上使用flow。我最初犯了同样的错误:P
  • 啊,是的,我忘了 Flow >
  • 谢谢,我认为它是普通的 java 脚本,因为它有一个 .js 扩展名而不是通常的 .ts

标签: reactjs


【解决方案1】:

类型检查!这是为您的组件设置类型。这反过来将 IntelliSense/Typechecking 添加到您的代码中(如果您的 IDE 支持它)。

您链接的示例使用流。如果你想了解更多结帐https://flow.org/

【讨论】:

    【解决方案2】:

    它是一种类型检查。在 TypeScript/flow (for react) 中,你已经为你的 props 和 states 指定了类型。 在给定的组件 ScrollMemory 中,ScrollProps 是传递给它的道具的类型

    type ScrollProps = {
      location: Object, // type of location must be object
      elementID?: string // elementID is optional, but if passed it should be string
    };
    

    相关问题:react/typescript: Parameter 'props' implicitly has an 'any' type error

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2018-07-06
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多