[英]TypeScript error when setting a component's state: Argument of type 'X' is not assignable to parameter of type '() => void'
I am having a TypeScript error when calling setState
:调用
setState
时出现 TypeScript 错误:
That's what I am actually doing:这就是我实际在做的事情:
updateRequests(requests: any, cb:Function|null = null) {
this.setState(
{
requests: {
...this.state.requests,
items: requests,
loading: false,
},
},
() => {
if (cb) cb();
}
);
Here's a Component state:这是一个组件状态:
export interface IServersScreenState {
requests: {
items: [];
loading: boolean;
statuses: {};
page: number;
lastPage: number;
scrollLoading: boolean;
};
}
Don't actually understand why it's happening like that, because I don't have any declaration in the state, that requests
needs to be look like ()=>void
实际上不明白为什么会这样,因为我在状态中没有任何声明,
requests
需要看起来像()=>void
here's my setState:这是我的setState:
setState<T>(...args: T[]) {
if (this.__mounted__) {
//@ts-expect-error spread error
super.setState(...args) as $TSFixMe;
}
}
You've defined setState to accept a generic, and then aren't filling in the generic when it's called.您已将 setState 定义为接受泛型,然后在调用泛型时不填写该泛型。 Try replacing
尝试更换
this.setState(
with和
this.setState<IServersScreenState>(
That should get you closer to following the type you've set up.这应该让您更接近于遵循您设置的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.