Fix having to cast web3.eth.Contract as unknown before casting to generated type #802
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, when using typechain generated types, one would have to cast to
unknown
orany
before casting to the generated Typechain type. For example:This is because Typescript did not consider
eth.Contract
and the generated type (ERC20
in this case) overlapping. There are two reasons for this.{ [string:key]: any }
in web3-v1, which does not contain the properties of generated events. For example:Proposed Changes
First, remove the constructor from the interface implemented by class instances, and move it into a separate interface meant for typing the static Class:
The factory/constructor interface may come in handy for mocking objects in testing, typing higher order functions, etc.
Second, we want to resolve the type mismatch in event callbacks, specifically in the Once methods -
For now, we can solve this issue by making the return values optional like this:
The above is a stopgap solution until I can either confirm or be denied from changing web3-v1's types upstream. It's not ideal, as we know for a fact that the event WILL contain the properties, but are forced into making them partial to satisfy web3-v1's narrow types.
A benefit of this PR is that users will be able to use the satisfies keyword going forward.
This keyword provided no benefit before as casting to
unknown
/any
destroys the underlying information of the type.Related PRs/Issues: web3/web3.js#2461 (we may be able to get this type changed again to
any
orobject
type, which would allow us to not have to make returnValues asPartial
.Other TODOs:
Bump the version of web3-v1 required for this PR to 1.8?