Search code examples
typescripttypeorm

Type 'Promise<void>' is not assignable to type 'Promise<xxx>'


I tryed following transaction in typescript. But it omit compile error like below. it says Type 'Promise<void>' is not assignable to type 'Promise<transactionArgument>' but this function return transaction which type is transactionArgument

So I wonder why this occured, if someone has opinion,will you please let me know

type RegisterPricingTransactionInfo = (pricingTransaction:transactionArgument,createdAt:Date,createdBy:string,datasource:any) => Promise<transactionArgument>

export const  registerPricingTransactionInfo: RegisterPricingTransactionInfo = async (pricingTransaction,createdAt,createdBy,datasource) => {
    let urlCode;
    let transaction:transactionArgument;
    transaction = {test:"test"}

     await datasource.manager.transaction(async (transactionalEntityManager:EntityManager) =>{
     try {
        await transactionalEntityManager.save(transaction)
        return transaction;
     } catch (error) {
        console.error(error)
     }
    })
}
    "message": "Type '(pricingTransaction: transactionArgument, createdAt: Date, createdBy: string, datasource: any) => Promise<void>' is not assignable to type 'RegisterPricingTransactionInfo'.\n  Type 'Promise<void>' is not assignable to type 'Promise<transactionArgument>'.\n    Type 'void' is not assignable to type 'transactionArgument'.",

Thanks..


Solution

  • You have a nested callback. It properly returns data, but the parent function does not.

    Proper code indentation may help spot the issue:

    const registerPricingTransactionInfo: RegisterPricingTransactionInfo = async (pricingTransaction,createdAt,createdBy,datasource) => {
      let urlCode;
      let transaction:transactionArgument;
      transaction = {test: "test"}
    
      await datasource.manager.transaction(async (transactionalEntityManager: EntityManager) => {
        try {
          await transactionalEntityManager.save(transaction)
          return transaction; // nested callback return
        } catch (error) {
          console.error(error)
      });
      // no return?
    }