简体   繁体   English

TypeScript 错误 - 类型 'Ticket[] | undefined' 不是数组类型

[英]TypeScript Error - Type 'Ticket[] | undefined' is not an array type

How do I append an item to an array of objects to a state (React) while using TS?如何在使用 TS 时将项目 append 到 state (React) 的对象数组?

This is my code:这是我的代码:

export type AppState = {
    tickets?: Ticket[],
}

export type Ticket = {
    id: string,
    title: string;
}

export type ApiC = {
    getTickets: (order:string, page:number) => Promise<Ticket[]>;
}

export class App extends React.PureComponent<{}, AppState> {

    state: AppState = {
    }


   fetchNewData = async () => {
        return await api.getTickets(this.state.currentOrder, this.state.page)
   }

    handleLoadMore = () => {

       let addData:Promise<Ticket[]> = this.fetchNewData()

        if (addData) 
            this.setState({
                tickets: [...this.state.tickets, addData]
            })
    }

I keep getting this error:我不断收到此错误:

Type 'Ticket[] |输入'票[] | undefined' is not an array type. undefined' 不是数组类型。

I tried:我试过了:

let addData:Ticket[] = this.fetchNewData()

too and the error I get:也是我得到的错误:

Type 'Promise<Ticket[]>' is missing the following properties from type 'Ticket[]': length, pop, push, concat, and 28 more.类型 'Promise<Ticket[]>' 缺少类型 'Ticket[]' 的以下属性:length、pop、push、concat 和另外 28 个。 Type 'Ticket[] |输入'票[] | undefined' is not an array type. undefined' 不是数组类型。

I tried writing it in all kind of variations with no luck.我试着用各种变体写它,但没有运气。

Both pointing error in this line :此行中的两个指向错误

  > 144 |               tickets: [...this.state.tickets, addData]
        |                            ^

There are few problems in your code, you can fix those:您的代码中存在一些问题,您可以解决这些问题:

  1. Remove the optional from state and initialize it with empty array从 state 中删除可选项并用空数组初始化它
export type AppState = {
  tickets: Ticket[] // remove the ?
  // ...
}

constructor(props) {
  super(props)
  this.state = {
    tickets: [],
    // ...
  }
}
  1. Assuming addData is array of Ticket , you need to make handleLoadMore function async to use await on this.fetchNewData to get the data returned by it:假设addDataTicket的数组,您需要使handleLoadMore function async ,以便在this.fetchNewData上使用await来获取它返回的数据:
handleLoadMore = async () => {
  let addData = await this.fetchNewData()

  if (addData)
    this.setState({
      tickets: [...this.state.tickets, ...addData],
    })
    // Or
    this.setState({
      tickets: this.state.tickets.concat(addData),
    })
}

You should destructure adddata as well.您还应该解构adddata

tickets: [...this.state.tickets, ...addData]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿错误:类型&#39;undefined []&#39;不能分配给&#39;void&#39;类型 - Typescript error: Type 'undefined[]' is not assignable to type 'void' 打字稿(类型“未定义”不可分配给类型)为什么我的数组有一个 | 不明确的? - Typescript (Type 'undefined' is not assignable to type) Why is my Array having a | undefined? TypeScript(Nodejs 错误) 输入 'string | undefined' 不可分配给类型 'string'。 类型“undefined”不可分配给类型“string” - TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string 打字稿对此没有定义。 类型 - Typescript undefined this. type 输入&#39;字符串| null&#39; 不能分配给类型 &#39;string | TypeScript 的未定义错误 - Type 'string | null' is not assignable to type 'string | undefined error with TypeScript 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error TypeScript将类型保存为未定义 - TypeScript saves type as undefined Typescript 变量类型和未定义 - Typescript variable with type and undefined React/Redux 减速器打字稿错误(“未定义”类型不可分配给 ISupplierState 类型) - React/Redux reducer typescript error (Type 'undefined' is not assignable to type ISupplierState) React / Typescript:将 obj 推入 object 和未定义类型的数组 - React / Typescript : pushing obj into array of object and undefined type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM