异步功能错误:“类型'Promise <MyType []> []'无法分配给类型'MyType []'”

时间:2020-08-27 11:01:18

标签: typescript

此函数在转换为异步之前是可以的,然后我的类型注释自尝试进行纠正以来一直未同步:

export default async function formatCallRecordsForPGPromise(
  rawCalldata: CallRecord[],
): Promise<SaveableCallRecord[]> {
  const dataSortedbyPhoneNumber: PhoneNumberKeyedCallData = lodash.groupBy(rawCalldata,
    (record: CallRecord) => record.destination);

  const phoneNumberAndGroupedData: [string, CallRecord[]][] = Object
    .entries(dataSortedbyPhoneNumber);

  const allData: Promise<SaveableCallRecord[]>[] = phoneNumberAndGroupedData
    .map(async (numberAndCallData: [string, CallRecord[]]) => {
      const phoneNumber = numberAndCallData[0];

      const selectCampaignId = 'SELECT id FROM campaign WHERE phoneNumber = $1';

      let campaign_id: number;
      try {
        campaign_id = await pgp.configured.one(
          selectCampaignId, phoneNumber, (queryResult: { id: number }) => queryResult.id,
        );
      } catch (error) {
        throw new Error(error);
      }

      const formattedDataSet: SaveableCallRecord[] = numberAndCallData[1]
        .map((record: CallRecord) => {
          const {
            date, callerid, destination, description, account, disposition, seconds, uniqueid,
          } = record;

          const formattedData: SaveableCallRecord = {
            unique_id: uniqueid,
            caller_id: callerid,
            <more 
          };

          return formattedData;
        });

      return formattedDataSet;
    });

  const allDataFlattened = allData.flat();

  return allDataFlattened;
}

错误:

/*
const allDataFlattened: Promise<SaveableCallRecord[]>[]
Type 'Promise<SaveableCallRecord[]>[]' is not assignable to type 'SaveableCallRecord[]'.

Type 'Promise<SaveableCallRecord[]>' is missing the following properties from type 'SaveableCallRecord': unique_id, caller_id, date, description, and 4 more.ts(2322)
*/

我尝试了一些小调整,但没有皮棉错误就无法通过注释。所需的返回类型是SaveableCallRecord的数组。谁能看到这个问题?

更新

  const allDataFlattened = (await allData).flat();

我收到此错误:

/*
const allDataFlattened: Promise<SaveableCallRecord[]>[]
Type 'Promise<SaveableCallRecord[]>[]' is not assignable to type 'SaveableCallRecord[]'.
  Type 'Promise<SaveableCallRecord[]>' is missing the following properties from type 'SaveableCallRecord': unique_id, caller_id, date, description, and 4 more.ts(2322)
*/

更新2

  await Promise.all(allData);

  const allDataFlattened = allData.flat();

  return allDataFlattened;

错误保持不变。

尽管我继续尝试,但我仍然不确定如何解决此问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

一位同事告诉我,TSC编译器无法推断4 * vec + 1:4 #> 1.04 2.4 7 44 上的承诺解决方案。

旧代码

allData
await Promise.all(allData);

const allDataFlattened = allData.flat();

return allDataFlattened;