错误 TS2314:通用类型“组件<P,S>"需要 2 个类型参数 [英] error TS2314: Generic type &#39;Component&lt;P, S&gt;&#39; requires 2 type argument(s)

查看:27
本文介绍了错误 TS2314:通用类型“组件<P,S>"需要 2 个类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TypeScript 中使用 ReactJS 时,出现此错误:

In using ReactJS with TypeScript, this error comes up:

错误 TS2314:通用类型 'Component'需要 2 个类型参数.

我该如何解决这个问题?

How do I fix this?

推荐答案

P 是 props 类型,S 是 state 类型.你会想要改变:

The P is the props type and the S is the state type. You'll want to change:

class MyComponent extends React.Component { ...

到:

interface MyProps {}
interface MyState {}

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

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

Then expand the MyProps and MyState interfaces to include typing for all the props and state that the component needs.

这篇关于错误 TS2314:通用类型“组件<P,S>"需要 2 个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆