After converting this function to async, I've been encountering issues with type annotations being out of sync:
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;
}
Error:
/*
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)
*/
I have tried several minor tweaks but can't get the annotations to pass without lint errors. The desired return type is an array of SaveableCallRecord
. Can anyone see the issue?
Update
const allDataFlattened = (await allData).flat();
I get this error:
/*
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)
*/
Update 2
await Promise.all(allData);
const allDataFlattened = allData.flat();
return allDataFlattened;
Error remains the same.
I am still unsure how to solve this, although I continue to try. Any help is appreciated.