简体   繁体   English

设置组件状态时出现 TypeScript 错误:“X”类型的参数不可分配给“() => void”类型的参数

[英]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.

相关问题 TypeScript 错误 '(response) =&gt; void' 类型的参数不可分配给'(value: void) =&gt; void | 类型的参数承诺喜欢<void> '</void> - TypeScript error Argument of type '(response) => void' is not assignable to parameter of type '(value: void) => void | PromiseLike<void>' React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' 返回“void”类型参数的打字稿不能分配给“TimerHandler”类型的参数? - Typescript returning Argument of type 'void' is not assignable to parameter of type 'TimerHandler? 打字稿:X 类型的参数不能分配给 Y 和 X 类型的参数。X 类型不能分配给 Y 类型 - Typescript: Argument of type X is not assignable to parameter of type Y & X. Type X is not assignable to type Y TYPESCRIPT :类型“typeof X”的参数不能分配给类型为“Y”的带有扩展的参数 - TYPESCRIPT : Argument of type 'typeof X' is not assignable to parameter of type 'Y' with extends React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type 类型“ void”的参数不能分配给“功能”类型的参数 - Argument of type 'void' is not assignable to parameter of type 'Function' 打字稿错误:“对象”类型的参数无法分配给“ {} []”类型的参数 - Typescript error: Argument of type 'Object' is not assignable to parameter of type '{}[]' 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM