【问题标题】:Generic type 'Component<P, S>' requires 2 type argument(s)通用类型 'Component<P, S>' 需要 2 个类型参数
【发布时间】:2017-12-07 10:36:19
【问题描述】:

我正在尝试使用tsreact-router v4 做demo,会报错。

Failed to compile.

Error in D:\data\code\taven\react-components-usage-ts\node_modules\@types\react-router\lib\withRouter.d.ts

(3,99): error TS2314: Generic type 'PureComponent<P, S>' requires 2 type argument(s).

【问题讨论】:

  • 您是否向react-router repo 提出过问题?

标签: reactjs typescript react-router


【解决方案1】:

我也看到了这个错误。这个错误是在2周前的最新版本4.0.12版本中出现的。使用以前的版本。在里面一切正常。

npm install --save @types/react-router@4.0.11

同样的事情也发生在 react-route-dom 上。使用:

npm install --save @types/react-router-dom@4.0.4

【讨论】:

  • 我不想使用新路由器,很难更新到新版本。那么在这种情况下该怎么办呢?
【解决方案2】:

您缺少组件上方的道具和状态的接口。

interface P {
    sampleProp: string,
    awesomeProp2: number

}
interface S {}

PureComponent<P, S>

【讨论】:

  • 当我构建项目时,错误将来自react-router lib
  • 您是否尝试过将 PureComponent&lt;P, S&gt; 更改为 PureComponent&lt;any, any&gt; 将错误限制为 react-router 定义可能会有所帮助。
  • PureComponent 来自react-router,我导入了react-router
【解决方案3】:

你在使用 React-Redux 吗?这是几天前更新的。我不得不恢复到旧版本。

https://github.com/DefinitelyTyped/DefinitelyTyped/blame/master/types/react-redux/index.d.ts#L179

这个版本对我有用。 "@types/react-redux": "4.4.32"

【讨论】:

  • @types/react 是构建 typescript 2.1,它是固定使用 typescript 2.3。我不知道为什么
猜你喜欢
  • 2015-12-06
  • 2018-11-13
  • 1970-01-01
  • 2020-07-08
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多