Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of type 'unknown' is not assignable to parameter of type 'string'. TS2345

I am running this code and have just seen this error appear.

import { ethers } from 'ethers'
import { getMulticallContract } from 'utils/contractHelpers'
import { MultiCallResponse } from './types'

export interface Call {
  address: string // Address of the contract
  name: string // Function name on the contract (example: balanceOf)
  params?: any[] // Function params
}

interface MulticallOptions {
  requireSuccess?: boolean
}

const multicall = async <T = any>(abi: any[], calls: Call[]): Promise<T> => {
  try {
    const multi = getMulticallContract()
    const itf = new ethers.utils.Interface(abi)

    const calldata = calls.map((call) => [call.address.toLowerCase(), itf.encodeFunctionData(call.name, call.params)])
    const { returnData } = await multi.aggregate(calldata)

    const res = returnData.map((call, i) => itf.decodeFunctionResult(calls[i].name, call))
    

    return res
  } catch (error) {
    throw new Error(error)
  }
}

This is the error that I'm getting, although until now this has been running just fine.

Argument of type 'unknown' is not assignable to parameter of type 'string'.  TS2345

    28 |     return res
    29 |   } catch (error) {
  > 30 |     throw new Error(error)
       |                     ^
    31 |   }
    32 | }
    33 |

I've looked through related questions, but can't see any exact solution in this case, but also I'm learning typescript currently.

like image 432
DanielSon Avatar asked Sep 08 '25 05:09

DanielSon


1 Answers

In the end I just added "useUnknownInCatchVariables": false, to tsconfig and it stopped errors from being converted to unknown, which is what was causing this issue.

I believe I have to upgrade typescript as well though, because there is a warning in tsconfig about this flag Unknown compiler option 'useUnknownInCatchVariables'.

Also see that another solution to this is mentioned in the docs: https://www.typescriptlang.org/tsconfig#useUnknownInCatchVariables

try {
  // ...
} catch (err) {
  // We have to verify err is an
  // error before using it as one.
  if (err instanceof Error) {
    console.log(err.message);
  }
}
like image 192
DanielSon Avatar answered Sep 10 '25 10:09

DanielSon