小编Caz*_*tto的帖子

如何在 React PropTypes 中使用 Typescript Union Type?

我正在尝试做类似的事情:

interface Props extends RouteComponentProps {
  country: 'ONE' | 'OTHER;
}
MyComponent.propTypes = {
  country: PropTypes.oneOf(['ONE', 'OTHER']).isRequired,
};
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Type 'Validator<string>' is not assignable to type 'Validator<"ONE" | "OTHER">'.
  Type 'string' is not assignable to type '"ONE" | "OTHER"'.ts(2322)
Run Code Online (Sandbox Code Playgroud)

两个疑问:

  1. 如何使用 PropTypes 键入它?
  2. 在 TypeScript Create React APP 应用程序中是否有更简单的方法来使用 Typescript 和 PropTypes?

types typescript reactjs create-react-app react-proptypes

3
推荐指数
1
解决办法
2577
查看次数