-
Notifications
You must be signed in to change notification settings - Fork 361
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
[ethers-v5] Contract types not exported in generated index.ts #278
Comments
I did try this in my PR but faced some typescript errors. I had two ways of doing this: 1. Export both definations and factories from
|
@zemse do you still have a branch with that try hanging around? It should be:
not
|
I was using I tried with |
Does this line in your |
This change works for me. - .map((fileName) => [`export * from './${fileName}Factory'`, `export * from './${fileName}.d'`].join('\n'))
+ .map((fileName) => [`export * from './${fileName}Factory'`, `export * from './${fileName}'`].join('\n')) But there is a catch. I know it only makes sense to export factories of contracts which are concrete, excluding abstracts and interfaces. But it makes sense to export types of interfaces. In fact when you cast a contract in your test code you should usually cast to the interface, not the concrete implementation. |
Hi, I looked into this, still getting error. I have created a minimal reproduction repo with typescript files. https://github.com/zemse/types-import-problem The typescript compiler doesn't give any error. But when you run the code with node js, it gives an error for the type file. Can you have a look? |
I see the problem. - path: join(this.outDirAbs, `${contract.name}.d.ts`),
+ path: join(this.outDirAbs, `${contract.name}.ts`), |
That really needs to be a type definition file because it only has method signatures and doesn't have method implementations. You can see that by renaming a generated I think it could be something unexpected happening on the typescript side (since TS compiler doesn't error) so I've created a thread on Typescript repo microsoft/TypeScript#40517. |
Even though it only defines types I think it should be a |
There are methods in it without implementations which doesn't make sense in a I think we would need the following structure:
I see this way it is possible. I can give a trial on this later this month. cc: @krzkaczor |
Can u help me to understand why it's needed at all? I just import directly from exact files. CC: @quezak :) |
Imagine you want to create a reusable npm package with the code generated by TypeChain, instead of generating it on demand in every project you want to use it. In that case you want a package.json main key and a typescript types key with everything you typically need in the usage of package, which includes the contract types, so you can just do In the current state you have to do some trickery by copying the |
I agree that adding a barrel file |
Okay, I get it now. @zemse it would be cool if you could work on this. |
Planning to work on it this weekend
I did not have luck with having I thinking to go ahead with this solution #278 (comment). And with this pattern, I think this would be a breaking change since there would be |
IMHO you should generate typescript code, and let the |
I'm using a private npm package where the TypeChain-generated code with
I'm seeing if I can get Webpack to ignore the |
I am stucking the same thing, have you found the answer? |
The main issue here is that the index.ts file generated by typechain is blank:
If you could add all the exports:
It would work perfectly, as you'd only need to point your main file to be dist/index.js, which will have all this generated types. Sadly it's not working for me, I'm just getting a plain index.ts file using @typechain/ethers-v5. |
Got it to "work" with this: import { writeFileSync } from 'fs'
import { glob } from 'glob'
const main = async () => {
const files = glob
.sync('./types/ethers-contracts/*.ts', {
ignore: ['./types/ethers-contracts/index.ts']
})
.map(file => file.replace('./types/ethers-contracts/', './'))
.map(file => file.replace('.d', ''))
.map(file => file.replace('.ts', ''))
const exports = files.map(fileName =>
[`export * from '${fileName}'`].join('\n')
)
const content =
'/* eslint-disable node/no-missing-import */\n' +
exports.join('\n') +
'\n'
writeFileSync('./types/ethers-contracts/index.ts', content)
}
main() The problem that I'm seeing now is that the generated .d.ts files don't have their interface typed properly (for example, I have a DAI contract that should have a interface DaiInterface extends ethers.utils.Interface {
functions: {};
events: {};
} How did you guys made it work properly? Maybe the ethers package version I'm using is wrong? {
"dependencies": {
"ethers": "^5.0.0"
},
"devDependencies": {
"@typechain/ethers-v5": "^10.1.0",
"eslint-config-base": "*",
"glob": "^8.0.3",
"ts-node": "^10.8.2",
"typescript": "^4.5.5"
}
} Update: For anyone experiencing the same this is probably an issue with the plugin hardhat-abi-exporter, just make sure to set the prettier option to false.
|
The index.ts only reexports the factory types, it does not export the contract types, the ones at
<ContractName>.d.ts
.These types should also be exported.
The text was updated successfully, but these errors were encountered: