首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在TypeScript中使用web3.js@1.0.0-beta34?

如何在TypeScript中使用web3.js@1.0.0-beta34?
EN

Ethereum用户
提问于 2018-04-29 15:13:46
回答 4查看 7.3K关注 0票数 10

下面是用于测试的最低限度代码:

代码语言:javascript
运行
复制
import * as Web3 from "web3"

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))

const main = async () => {
  const blockNumber = await web3.eth.getBlockNumber()
  console.log({ blockNumber })
}

main().catch(err => console.error(err))

模块类型是错误的。如果我试图编译它,就会出现一些错误:

代码语言:javascript
运行
复制
maxblock@mbp t10-web3-typescript *$ npm run build

> t10-web3-typescript@1.0.0 build /Users/maxblock/projects/test/t10-web3-typescript
> rimraf dist && tsc

../../../node_modules/web3/types.d.ts(1,27): error TS7016: Could not find a declaration file for module 'bn.js'. '/Users/maxblock/node_modules/bn.js/lib/bn.js' implicitly has an 'any' type.
  Try `npm install @types/bn.js` if it exists or add a new declaration (.d.ts) file containing `declare module 'bn.js';`
../../../node_modules/web3/types.d.ts(2,21): error TS7016: Could not find a declaration file for module 'underscore'. '/Users/maxblock/node_modules/underscore/underscore.js' implicitly has an 'any' type.
  Try `npm install @types/underscore` if it exists or add a new declaration (.d.ts) file containing `declare module 'underscore';`
src/index.ts(3,14): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
src/index.ts(3,32): error TS2339: Property 'providers' does not exist on type 'typeof "/Users/maxblock/node_modules/web3/index"'.

好的。接下来,我尝试用自己的类型重写类型。出于这个原因,我创建了一个带有简单存根的文件typings.d.ts:declare module "web3"

我自己的输入错误较少,但无论如何我无法编译它:

代码语言:javascript
运行
复制
maxblock@mbp t10-web3-typescript *$ npm run build

> t10-web3-typescript@1.0.0 build /Users/maxblock/projects/test/t10-web3-typescript
> rimraf dist && rimraf node_modules/web3/index.d.ts && rimraf node_modules/web3/types.d.ts && tsc

../../../node_modules/web3/types.d.ts(1,27): error TS7016: Could not find a declaration file for module 'bn.js'. '/Users/maxblock/node_modules/bn.js/lib/bn.js' implicitly has an 'any' type.
  Try `npm install @types/bn.js` if it exists or add a new declaration (.d.ts) file containing `declare module 'bn.js';`
../../../node_modules/web3/types.d.ts(2,21): error TS7016: Could not find a declaration file for module 'underscore'. '/Users/maxblock/node_modules/underscore/underscore.js' implicitly has an 'any' type.
  Try `npm install @types/underscore` if it exists or add a new declaration (.d.ts) file containing `declare module 'underscore';`

我甚至尝试过删除node_modules/web3/index.d.ts和node_modules/web3/ypes.d.ts文件,但没有成功。

如何使用web3.js + TypeScript?

EN

回答 4

Ethereum用户

发布于 2018-06-20 08:53:37

package.json所在的项目根文件夹下,运行以下命令:

代码语言:javascript
运行
复制
npm install --save-dev @types/underscore
npm install bignumber.js

代码语言:javascript
运行
复制
yarn add @types/underscore --dev
yarn add bignumber.js

编辑/node_moduoles/web3/types.d.ts的第一行如下:

代码语言:javascript
运行
复制
-- import { BigNumber } from 'bn.js'
++ import { BigNumber } from 'bignumber.js'

您的代码应该编译

票数 1
EN

Ethereum用户

发布于 2018-06-28 18:31:14

对我有用的是:

  1. 安装node类型。npm安装@type/节点--保存
  2. node类型中指定tsconfig.json:{ "compilerOptions":{ // rest,“类型”:},"typeRoots":}
  3. 使用require语法而不是import * as Web3 from 'web3';:const Web3 =Web3(‘web3’);
票数 1
EN

Ethereum用户

发布于 2018-11-16 04:18:09

您需要为提到的两个库underscorebn.js提供类型。第一个很简单,只需按它的建议运行npm install --save-dev @types/underscore。不幸的是,后者没有可用的@types/bn.js包。但是您可以找到它的类型定义,这里有一个可以复制到您的项目中:https://github.com/MyCryptoHQ/MyCrypto/blob/develop/common/typescript/bn.d.ts

您不应该编辑node_modules中的文件以使其正常工作,因为当您重新下载或更新项目中的模块时,这些更改不会对其他任何人或您产生影响。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/47055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档