【问题标题】:Type 'Promise<void>' is missing the following properties from type类型“Promise<void>”缺少类型中的以下属性
【发布时间】:2020-12-11 07:23:43
【问题描述】:

我不太理解以下错误:

Type 'Promise<void>' is missing the following properties from type 'InventoryModel[]': length, pop, push, concat, and 28 more.  TS2740

我有以下功能组件:

import React, { useEffect, useState } from "react";
import { Grid, TextField } from "@material-ui/core";
import InventoryService from "../services/Inventory";
import InventoryModel from "../Models/Inventory";
import { isThisTypeNode } from "typescript";

type InventoryState = {
  inventoryItems: InventoryModel[];
};

const Inventory: React.FC = () => {
  const [inventory, setInventory] = useState<Partial<InventoryState>>({});
  useEffect(() => {
    const inventoryService = new InventoryService();
    const results = inventoryService.getByLocation().then((response) => {
      setInventory({ ...inventory, inventoryItems: response });
    });
  }, []);
  return <div></div>

“inventoryItems”在这种情况下以红色突出显示。

【问题讨论】:

  • 如果您将(response) 参数更改为(response: InventoryModel[]),编译器可能会给您一个更可操作的错误
  • 感谢您的帮助

标签: reactjs typescript promise


【解决方案1】:

原来我只需要添加 |对 InventoryState 无效。

type InventoryState = {
  inventoryItems: InventoryModel[] | void;
};

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2021-01-14
    • 2021-04-02
    • 2021-04-20
    • 2021-07-29
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多