在使用 ReactJS 和 TypeScript 时,会出现以下错误:
error TS2314: Generic type 'Component<P, S>' requires 2 type argument(s).
我该如何解决这个问题?
P
是道具类型,S
是状态类型。你会想要改变:
class MyComponent extends React.Component { ...
至:
interface MyProps {}
interface MyState {}
class MyComponent extends React.Component<MyProps, MyState> { ...
然后展开
MyProps
和 MyState
接口以包含所有道具的输入并说明组件需要的状态。
如果在 typescript 编译项目的第 3 方库时发生此错误(实际上这不是您的错误),那么 tsconfig.json 中的此设置很有帮助
"compilerOptions": {
"skipLibCheck": true,
...
}