【问题标题】:error TS2314: Generic type 'Component<P, S>' requires 2 type argument(s)错误 TS2314:通用类型 'Component<P, S>' 需要 2 个类型参数
【发布时间】:2015-12-06 23:52:06
【问题描述】:

在将 ReactJS 与 TypeScript 一起使用时,出现此错误:

error TS2314: Generic type 'Component&lt;P, S&gt;' requires 2 type argument(s).

我该如何解决这个问题?

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    P 是道具类型,S 是状态类型。你会想要改变:

    class MyComponent extends React.Component { ...

    到:

    interface MyProps {}
    interface MyState {}
    
    class MyComponent extends React.Component<MyProps, MyState> { ...
    

    然后扩展 MyPropsMyState 接口以包括输入组件所需的所有道具和状态。

    【讨论】:

    • 扩展了 React.Component

    猜你喜欢
    • 2017-12-07
    • 2022-08-04
    • 2017-02-08
    • 2020-12-06
    • 2023-03-06
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多