4

I have set up a hardhat project with typescript a while ago and want to write some tests. Now I only have a typechain-types directory and it doesn't give me the types I am used to. Where is the plain typechain directory? For instance this following code:

import { expect } from "chai";
import { ethers } from "hardhat";

import type {
    Example
 } from "../typechain-types";

async function doSomeExample() {
    let example : Example
    const signers = await ethers.getSigners()
    const ExampleFactory = await ethers.getContractFactory('Example', signers[0])
    example = await ExampleFactory.deploy()
}

gives me the Type-error: Type 'Contract' is missing the following properties from type 'Example': ... in the pen-ultimate line.

I have - amongst others - installed the following packages in package.json (it is a bit of a mess, admittedly):

"devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.5",
    "@nomiclabs/hardhat-etherscan": "^3.0.3",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "@typechain/ethers-v5": "^10.1.0",
    "@typechain/hardhat": "^6.0.0",
    "@types/chai": "^4.3.1",
    "@types/mocha": "^9.1.1",
    "@types/node": "^17.0.31",
    "@typescript-eslint/eslint-plugin": "^5.22.0",
    "@typescript-eslint/parser": "^5.22.0",
    "ts-node": "^10.7.0",
    "typechain": "^8.1.0",
    "typescript": "^4.6.4",
    ...
},
"dependencies": {
    "@typechain/truffle-v5": "^8.0.1",
    "@typechain/web3-v1": "^6.0.1",
    "hardhat-typechain": "^0.3.5",
    "ts-generator": "^0.1.1",
    ...
}
   

My tsconfig.json looks as follows:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist",
    "declaration": true
  },
  "include": ["./scripts", "./test", "./typechain"],
  "files": ["./hardhat.config.ts"]
}

and the hardhat.cofig.ts starts with:

import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";

...

Also cleaning the types and all that with npx hardhat clean and re-compiling npx hardhat compile won't generate the types at the correct directory with the expected behaviour. What am I doing wrong?

1
  • 2
    Take a look at my Hardhat template, which comes pre-configured with TypeChain and should implicitly solve your problem. Commented Dec 6, 2022 at 10:01

3 Answers 3

4

You can define the typechain output target in the hardhat configuration file.

To receive the desired "plain" typechain directory add the following to your config file:

const config: HardhatUserConfig = {
solidity: {
    ...
  },
  typechain: {
    outDir: "typechain",
    target: "ethers-v5",
  },
  networks: {
    ...
  },
}
2

try to re-initial your hardhat project and use the default typescript template.

please vote me up if it's useful :)

1
  • Thanks, I yes thats what I ended up doing, but it feels like nodt the right way. There must be something in the configs that one can change
    – Marlo
    Commented Aug 4, 2022 at 16:11
2

You can do npx hardhat typechain and it will regenerate the folder for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.