-
Notifications
You must be signed in to change notification settings - Fork 5.1k
How to use [email protected] with TypeScript? #1597
Comments
Same issue, you need to delete first to imports inside web3 folder in types.d.ts, and they usages, BigNumber and underscore. |
I'm currently spec'ing out a TCR with web3 beta and TS. Feel free to checkout the https://github.com/computablelabs/computable.js I'm putting a couple PRs together now for TS corrections... |
@Spaider15 Could you clarify how to remove usages? |
@pau1m Find in file where they are mentioned and just remove it |
Hey all. I am sorry but I cannot follow. Can someone please reiterate for dummies like me on how to fix this? Thanks you very much in advance. |
As workaround you can use require syntax: There will be no typings, but it compiles. |
I am able to solve the above mentioned issue by importing as default.
Also, you may need to set |
@jintoppy Yes, it really compiles with "noImplicitAny": false. But for me it only compiles, can't run.
When I run it, there is an error:
Here is a link on repo with this example. Can you please check, does it work for you? |
The correct syntax to import Web3 is import Web3 = require('web3') because the module uses module.exports = Web3; However the type definitions are incorrect: export = Web3; |
Here is the commit breaking the type definitions c0a4f0a |
@levino This doesn't work for me, when I compile it, there is an error:
|
Which type definitions are you using? The ones from here are broken, as I am saying. It cannot work. You have to fix them by hand. |
Okay I "fixed" it for me locally until this madness is resolved properly. {
"postinstall": "rm -f node_modules/web3/index.d.ts"
} This will remove the incorrect type definitions shipped with web3. {
"typeRoots": [
"./node_modules/@types/",
"./typings/"
]
} |
my web3 typescript project works fine.. https://github.com/wmira/ethereum-notification/blob/master/src/index.ts#L2 |
What does your tsconfig look like? |
You use
While it might work for your case, I am not sure whether this implicit mapping also correctly works for the exported type. Like doing something like this import Web3 from 'web3'
let web3: Web3 There simply is a mismatch between the actual export object and what the type declarations say ( that export has a key default with a value, which is a lie). |
have you tried? my tsconfig is on that repo. |
You also have When you turn This package must work with a default and best practice typescript configuration. It is also possible as I have shown above. |
@levino use the declaration below: declare module 'bn.js' { import { Buffer } from 'buffer' type Endianness = 'le'|'be' class BN {
} export type BigNumber = BN } Taken from https://github.com/indutny/bn.js/blob/master/lib/bn.js . |
Dear @wmira, above I suggested a solution for the problem by replacing the non-working type definitions from the I also have been running around this repo trying to point the problems out. Seems everybody is sleeping. While I wait for someone to wake up I opened a PR to DefinitelyTyped to have high quality, linted and tested type definitions for I appreciate your follow up on the matter but I think we might also have a little bit of a misunderstanding here. I think the issue at hand is resolved. As for your repo, try removing this line. You then have to import Web3 via Also if you have to add typings for a dependency of I see that you know what you are doing when it comes to typescript. I would really appreciate a review or some further contributions (like more test code) to the PR in DefinitelyTyped and some support for my move to remove the type definitions from here. |
@levino i know where you are coming from but sometimes we need to get things going as we need to move and write code. The copy paste of the BN typings is not for the sake of doing so -- obviously a better type can be created but that is not the point here. I'm sure the people here are busy and so are you so lets take it easy please. |
I am taking it easy. I did not mean any offense. I understand that everyone is busy and I have no problem to wait until the this topic is picked up by a maintainer (I think sleeping well and long is very important and was not implying that anyone is lazy, but just not working at the moment, maybe they have a team vacation or something). I am merely offering my help. BTW |
Also type definitions for |
Also highlighting @LogvinovLeon to get his view on the matter. |
@levino are you able to just do a pull request to fix the type definitions here? instead of removing them? |
@levino Correct me if I'm wrong, but as far as I know - web3 maintainers don't see a value in using/supporting typescript. They can sporadically accept some PR's but don't have the expertice/motivation to do that. I think it will make more sense to keep the typings separately in that case (in DT) so that a review process is much more quick (DT maintainers are much faster). I'm now migrating our typings repository from 0x monorepo to DT. We have full web3 types that us and a bunch of other projects have been using for more than a year. https://github.com/0xProject/0x-monorepo/blob/v2-prototype/packages/typescript-typings/types/web3/index.d.ts I'm planning to introduce The only downside is that it supports only the latest stable version of web3. I think the beta branch is too immature. |
@wmira Yes, I could. But I do not want to. I think it is better to stop providing type definitions here.
They do accept PRs without understanding the consquences and without proper review. Also no linting is been applied, not to even think about tests for the type definitions. This repo is currently following an anti pattern. This is why I am pushing for the removal of the type definitions. Off topic: What are your issues with |
@levino Got it. Then I think removing types will be beneficial. |
Here is an instruction on how to use |
Btw. if anyone is curious how to work with smart contracts in a type-safe manner, TypeChain supports it! Just use: |
The best is to create a plugin to share through your components. window.web3 = window.web3 || {}; |
Or you just use JavaScript if you do not need types... |
Anyone has experience with the Typechain? |
Typechain does not solve this problem. It is for something else (typing the methods of the Contract instance from web3). I personally recommend |
"esModuleInterop": true in tsconfig.json and import Web3 from 'web3'; |
This workaround is currently working for Web3.js v1.9.0, in times when the web3.js team are preparing their v4.0.0 to be relased soon. I tested v4 and the problems are worst in Typescript with ESM, I do not know what to think, but I am using web3 instead of ethers because actually web3 retreives data from JsonRPC faster than ethers v5. @levino thanks you for this contribution. |
Uh oh!
There was an error while loading. Please reload this page.
Here is a minimal code for testing:
The module typings are wrong. If I try to compile it, there are some errors:
OK. Next, I tried to override typings with my own. For this reason, I've created a file typings.d.ts with simple stub:
declare module "web3"
.There are fewer errors with my own typings, but anyway I can't compile it:
I've even tried deleting node_modules/web3/index.d.ts and node_modules/web3/types.d.ts files, but without success.
How do you work with web3.js + TypeScript?
P.S.
If don't use import syntax and get Web3 like
const Web3 = require("web3")
, it can be compiled.The text was updated successfully, but these errors were encountered: