【问题标题】:Element implicitly has an 'any' type because expression of type 'any' can't be used to index type in nodejs and solidity元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引nodejs和solidity中的类型
【发布时间】:2021-11-11 06:41:42
【问题描述】:

我需要在我的合同中设置网络。我使用 nodejs 并输入脚本。

我写了这段代码:

    let networkId: any = await global.web3.eth.net.getId();
    let tetherData = await Tether.networks[networkId];

但它告诉我这个错误:

元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型“{}”.ts(7053)

现在我该如何解决这个问题?

这是我的合同的 json 文件

【问题讨论】:

  • 添加let tetherData:any ?
  • @JeremyThille 还是有问题

标签: javascript node.js typescript solidity


【解决方案1】:
let networkId: number = await global.web3.eth.net.getId();
let tetherData = await Tether.networks[(networkId as number)];

原因:

getId(callback?: (error: Error, id: number) => void): Promise<number>;
                                                              ^^^^^^

https://github.com/ChainSafe/web3.js/blob/ed1011438c3a7e35da8390998f6d1cb9b34db014/packages/web3-core/types/index.d.ts#L253

【讨论】:

  • 仍然显示那个错误
  • 我使用 web3js 而不是 web3-core
  • web3-core 是 web3js 的依赖
  • 我还是有问题。我在这一行的问题:let tetherData = await Tether.networks[networkId];
  • as number更新了答案
猜你喜欢
  • 2022-01-17
  • 2022-10-08
  • 2020-11-14
  • 1970-01-01
  • 2022-08-17
  • 2022-11-10
  • 2021-12-24
  • 2019-11-24
  • 2023-01-09
相关资源
最近更新 更多