Skip to content
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

Exporting Typechain definitions #4

Open
aciceri opened this issue Oct 14, 2021 · 5 comments
Open

Exporting Typechain definitions #4

aciceri opened this issue Oct 14, 2021 · 5 comments

Comments

@aciceri
Copy link

aciceri commented Oct 14, 2021

I think this is the best hardhat template around, really cool, everything just working out of the box.

However, besides the smart contracts package, I'm also writing an SDK package that imports and use my contracts and I would like to use (and expose from my SDK) the typechain definitions too.

Despite I think this is a common use case I didn't find anyone doing so, how can I do?

For now I tried adding my contracts package as dev dependency in my sdk dependencies but when I try to import something with:

import {MyContract__factory} from 'contracts-package/typechain';

I get the following trying to run with mocha --require ts-node/register 'src/test/**/*.ts'

SyntaxError: Unexpected token 'export'

It refers to the export in the autogenerated files by Typechain.

Instead, if I import the typechain definitions specifying the path on my machine with import {MyContract__factory} from 'absolute/path/to/my-package/typechain'; , it correcly works.

I'm not sure the problem is related to this repository, maybe simply I'm not able to import the typechain definitions and in this case I apologize for writing here. But maybe, in some way, the changes to make my idea possible have to be done here. And I think it would be a really nice improvement.

@wighawag
Copy link
Owner

wighawag commented Oct 15, 2021

you might need to add this to tsconfig

 "include": [
    "node_modules/<sdk>/typechain/**/*"
  ]

Not sure, might want to read on typescript/javascript. I am interested to know too :)

@aciceri
Copy link
Author

aciceri commented Oct 18, 2021

I think you mean to add an "include": ["./node_modules/contracts-package/typechain/**/*"] in the SDK's .tsconfig, right?

Anyway I already tried and I got the same error. Furthermore I just noticed that I get the error only if I try to import a factory, if I directly import the "contract type" (exported by Typechain with export type, not only export) it works.
In the end I managed to get what I wanted importing the artifact json, creating an ether.js factory and casting the objects it returns on .deploy() to the type generated by Typechain. An unsatisfactory solution.

This is weird since the error seems occurring at the beginning of the import, as soon as it tries to parse the typechain index.ts.

@wighawag
Copy link
Owner

not in the sdk package, in the app using the sdk.

@aciceri
Copy link
Author

aciceri commented Oct 20, 2021

Wait a moment, I think I said it badly. I've this situation:

  • a package with contracts (created forking this template), let call it package A
  • a package with the sdk, let call it package B
  • a package (a react app) with the frontend, let call it package C

The frontend doesn't directly interact with contracts, it imports the sdk, which in turn imports the contracts.
The imports chain is: package C -> package B -> package A

Let exclude package C from the discussion since it has nothing to do with my problem, I tried putting your include snippet in the .tsconfig inside package B (i.e. the sdk) but I still get the same error.
However, as a I said in my previous message, I realized that my sdk (package B) can import typechain's contract types from the contracts pacakge (package A) but not the ether.js factory objects generated by typechain as well.

@mrwillis
Copy link

mrwillis commented Mar 9, 2022

@aciceri have you tried just generating headers via "declaration": true? Then consumers can still get a TS experience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants