Web3js
如何將 web3.js@1.0.0-beta34 與 TypeScript 一起使用?
這是用於測試的最小程式碼:
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/types.d.ts 文件,但沒有成功。
你如何使用 web3.js + TypeScript?
在您所在的項目根文件夾下
package.json
,執行以下命令:npm install --save-dev @types/underscore npm install bignumber.js
或者
yarn add @types/underscore --dev yarn add bignumber.js
編輯第一行
<rootDir>/node_moduoles/web3/types.d.ts
如下:-- import { BigNumber } from 'bn.js' ++ import { BigNumber } from 'bignumber.js'
你的程式碼應該編譯
對我有用的是:
- 安裝
node
類型。npm install @types/node --save
- 在 中指定
node
類型tsconfig.json
:{ "compilerOptions": { // rest ommitted, "types": ["node"] }, "typeRoots": [ "./node_modules/@types" ] }
- 使用
require
語法而不是import * as Web3 from 'web3';
:const Web3 = require('web3');