如何为道具定义类型

How to define type for props

代码:

let component = ReasonReact.statelessComponent("Page");
type matchParams = {.
  id: int
};
type match = {.
  params: matchParams
};
type userProps = {.
  match: match
};
let home = <Home />;
let user = (~props:userProps) => <User id=props.match.params.id />;
let make = (_children) => {
  ...component,
  render: (_self) =>
    <div> <Route key="home" exact=false path="/home" component=home />
    <Route key="user" exact=false path="/user/:id" component=user /> </div>
};

输出:

Unbound record field match

let user = (~props:userProps) => <User id=props.match.params.id />;

如何定义类型,我做错了什么?

错误是由于match是一个保留关键字。不过你应该得到更好的错误消息,我认为这是 Reason 中的错误。解决办法,如果需要在JS端编译成match,可以用_match代替,否则换个名字就可以了。

您(可能)还有一些其他问题:

  1. let home = <Home /> 不是像 user 这样的函数。它可能需要 let home = () => <Home/>

  2. 您将记录类型定义为 props,它将由 component 函数提供给您。但是你可能会得到一个 JS 对象而不是记录。请参阅解释差异的 Reason guide