-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement static type interface for Contract #4604
Conversation
8877b33
to
98c7bc4
Compare
@@ -0,0 +1,7 @@ | |||
// TODO: Convert it to static file | |||
type _SolidityIndexRange = 1 | 2 | 3 | 4 | 5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the fixed array size is passed through the ABI as string so we need relevant map for integer values. We can set this up to maximum we want to support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But isn't the range X >= 0? So it's some very large number that we'd have to support up to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically ECMA-262 supports 2^32 -1 which is very less than what solidity supports. We can add as much number we want, but the larger numbers here will make the intelisence of TS very slow. So we have to restrict this number to some limited values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the limitation reference in the README file.
packages/web3-eth-contract/test/fixtures/types/web3-v1-contracts/types.ts
Outdated
Show resolved
Hide resolved
Please test type checking support for famous community contracts:
Gnosis :
|
@jdevcs I had added type tests for ERC20 and ERC721. I believe these are good enough to test the type interface for contract. Gnosis safe/multisig is splited among different contracts, if you think think that's necessary to add please share reference to its ABI to use. |
For now just include ERC777 along with , including multisig in this PR will increase scope/time , add that in tests TODO items. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still don't fully understand all the TypeScript going on, but the tests seem to show it works as needed. I think we should find some more complex contracts to throw at this before releasing 4.x
@spacesailor24 Already created an issue to add more complex contract types tests. #4631 |
Co-authored-by: Wyatt Barnes <[email protected]>
…s into nh/4562-contract-typing
? Address | ||
: never; | ||
|
||
export type PrimitiveStringType<Type extends string> = Type extends `string${string}[${infer Size}]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love to have extensive JSDocs here explaining the rationale of this types. For external contributors this is hard to read and comments would help a lot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there benchmarks of the compilation cost of extensive use of Template Literal Types? Docs say:
We generally recommend that people use ahead-of-time generation for large string unions, but this is useful in smaller cases. ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never found any such, but these types provides real-time developer feedback in IDE so fast enough to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any demo project I can try with this feature? Would love to see some usage guide too
To better understand this PR, please correct me if I'm wrong:
|
Very impressive use of Template Literal Types 👍 technically this is really cool |
Description
Add contract static type support.
Fixes #4562
Type of change
Checklist:
npm run dtslint
with success and extended the tests and types if necessary.npm run test:unit
with success.npm run test:cov
and my test cases cover all the lines and branches of the added code.npm run build
and testeddist/web3.min.js
in a browser.CHANGELOG.md
file in the root folder.