Skip to content
Advertisement

Typescript, return type not assignable to another

I don’t know how to reponse correctly my component.

I’m getting this error:

Type 'Promise<AnyTransaction>[]' is not assignable to type 'AnyTransaction[]'.
Type 'Promise<AnyTransaction>' is missing the following properties from type 'Transaction<any, any, any, any>': id, feeLookup, sender, receiver, and 10 more.

The component looks like this:

import { AnyTransaction, TransactionStatus } from '../types';
// more code...
export default async function getMyTransactions (
  _parent: unknown,
  _args: unknown,
  // context: Context,
): Promise<Array<AnyTransaction>> {
// ): Promise<String> {
  // some code
  
  const { Items } = await DocumentClient.getInstance()
    .query(some query
    .promise();
  const transactions = (Items || []) as Array<AnyTransaction>;
  
  // transactions is an array of objects.
  
  return transactions.map(parseDeprecatedStatuses).map(processTransactionStatus);
  
  // parseDeprecatedStatuses: just parse some data
  // processTransactionStatus code is below
}

// processTransactionStatus.ts:

import Factory from '../factory';
import { AnyTransaction } from '../types';
export default async function processTransactionStatus (
  transaction: AnyTransaction
): Promise<AnyTransaction>{
  const agent = Factory.buildAgentFromCode(transaction.destination.agent);
  transaction.status = await agent.fetchTransactionStatus(transaction)
  return transaction;
}

I’m really confused about how I’m returning from the component and what I got.

Advertisement

Answer

You are mapping to a list of promises, so you have to await all of those:

return await Promise.all(transactions
    .map(parseDeprecatedStatuses)
    .map(processTransactionStatus));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement