You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The only way to enable correct type definition was to cast it to ContractFunction, which is kinda ugly as the function name and its parameters are too far apart:
// #1constbalance=await(usdc.balanceOfasContractFunction<BigNumber>)(walletAddress)// #2(usdc.balanceOfasContractFunction<BigNumber>)(walletAddress).then(balance=> ...)// #3constbalanceOf: ContractFunction<BigNumber>=usdc.balanceOf..constbalance=awaitbalanceOf(walletAddress)// Losing context of what contract we are working on
There is a .functions bucket that has a type of ContractFunction on each function, though it will not collapse single return value like the functions on root.
┌─Yes!Correcttypedefinition│usdc.functions.balanceOf(walletAddress).then(balance=>console.log(balance))│└─Butit's a Result array 😢
I played around and realized that there's no way we can make the meta-class functions behave to like we want, as index signature will override all of them with any.
So perhaps could we make another bucket like .methods or such, with a better type definitions and also collapse values for us?
┌─Yes!Correcttypedefinition│usdc.methods.balanceOf(walletAddress).then(balance=>console.log(balance))│└─Singlevalue is collapsed👍
I know that adding .methods might confuse a lot of people and might not be a good idea 😅, so I wanna open this discussion on how can we use ethers.js with TypeScript more peacefully without relying on other libs/extensions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Having all callable functions as a meta-class on root is a very clever idea:
However, having the index signature of
ContractFunction | any
on theContract
class also makes every function inherits the type ofany
.ethers.js/packages/contracts/src.ts/index.ts
Lines 1140 to 1143 in c80fcdd
Resulting in a poor typing when calling functions directly from the contract.
Ideally, I would want to write it like this, with a correct TypeScript
Promise
definition / autocompletion:But in reality,
The only way to enable correct type definition was to cast it to
ContractFunction
, which is kinda ugly as the function name and its parameters are too far apart:There is a
.functions
bucket that has a type ofContractFunction
on each function, though it will not collapse single return value like the functions on root.I played around and realized that there's no way we can make the meta-class functions behave to like we want, as index signature will override all of them with
any
.So perhaps could we make another bucket like
.methods
or such, with a better type definitions and also collapse values for us?I know that adding
.methods
might confuse a lot of people and might not be a good idea 😅, so I wanna open this discussion on how can we useethers.js
with TypeScript more peacefully without relying on other libs/extensions.Beta Was this translation helpful? Give feedback.
All reactions