【发布时间】:2021-11-02 07:54:15
【问题描述】:
我正在运行这段代码,刚刚看到这个错误出现。
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)
}
}
这是我遇到的错误,尽管到目前为止它一直运行良好。
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 |
我已经查看了相关问题,但在这种情况下看不到任何确切的解决方案,而且我目前正在学习打字稿。
【问题讨论】:
-
如果你只是“抛出错误”而不是“抛出新的错误(错误)”怎么办
-
catch子句的意义何在?你希望try块中的代码抛出什么样的error?
标签: javascript reactjs typescript jsx