下面是用于测试的最低限度代码:
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))
模块类型是错误的。如果我试图编译它,就会出现一些错误:
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"
。
我自己的输入错误较少,但无论如何我无法编译它:
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?
发布于 2018-06-20 08:53:37
在package.json
所在的项目根文件夹下,运行以下命令:
npm install --save-dev @types/underscore
npm install bignumber.js
或
yarn add @types/underscore --dev
yarn add bignumber.js
编辑/node_moduoles/web3/types.d.ts
的第一行如下:
-- import { BigNumber } from 'bn.js'
++ import { BigNumber } from 'bignumber.js'
您的代码应该编译
发布于 2018-06-28 18:31:14
对我有用的是:
node
类型。npm安装@type/节点--保存node
类型中指定tsconfig.json
:{ "compilerOptions":{ // rest,“类型”:},"typeRoots":}require
语法而不是import * as Web3 from 'web3';
:const Web3 =Web3(‘web3’);发布于 2018-11-16 04:18:09
您需要为提到的两个库underscore
和bn.js
提供类型。第一个很简单,只需按它的建议运行npm install --save-dev @types/underscore
。不幸的是,后者没有可用的@types/bn.js
包。但是您可以找到它的类型定义,这里有一个可以复制到您的项目中:https://github.com/MyCryptoHQ/MyCrypto/blob/develop/common/typescript/bn.d.ts
您不应该编辑node_modules
中的文件以使其正常工作,因为当您重新下载或更新项目中的模块时,这些更改不会对其他任何人或您产生影响。
https://ethereum.stackexchange.com/questions/47055
复制相似问题