错误 TS2314:通用类型“Component<P, S>”需要 2 个类型参数

问题描述 投票:0回答:2

在使用 ReactJS 和 TypeScript 时,会出现以下错误:

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

我该如何解决这个问题?

reactjs typescript
2个回答
52
投票

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

class MyComponent extends React.Component { ...

至:

interface MyProps {}
interface MyState {}

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

然后展开

MyProps
MyState
接口以包含所有道具的输入并说明组件需要的状态。


0
投票

如果在 typescript 编译项目的第 3 方库时发生此错误(实际上这不是您的错误),那么 tsconfig.json 中的此设置很有帮助

  "compilerOptions": {
    "skipLibCheck": true,
   ...
  }
© www.soinside.com 2019 - 2024. All rights reserved.