3

I've been able to run truffle tests successfully using TypeScript, but when it comes to migration, I get stuck.

1) My current setup:

tsconfig.json

{
    "compilerOptions": {
      "target": "es5",                          
      "module": "commonjs",                    
      "lib": ["es6"],                     
      "allowJs": true,
      "outDir": "build",                          
      "rootDir": "src",
      "strict": true,         
      "noImplicitAny": true,
      "esModuleInterop": true,
      "resolveJsonModule": true,
      "types": ["@types/node", "@types/chai", "@types/mocha"],
      "typeRoots": ["./node_modules/@types", "./types"],
    },
  }

truffle-config.js

require("ts-node").register({
  files: true,
});
//...

2) Migration files:

1_initial_migration.js

const Migrations = artifacts.require("Migrations");

module.exports = function (deployer) {
  deployer.deploy(Migrations);
};

2_deploy_contracts.js

module.exports = require("./2_deploy_contracts.ts");

2_deploy_contracts.ts

const Fallback = artifacts.require('Fallback');

module.exports = async (
  deployer: Truffle.Deployer,
  network: string,
  accounts: string[]
) => deployer.deploy(Fallback);

3) Problem:

I am using an intermediate file 2_deploy_contracts.js that is calling 2_deploy_contracts.ts, since Truffle apparently is ignoring the ts migration files.

Problem is that some types are not defined when trying to deploy the contracts:

$ truffle deploy --network development --reset

Error description:

migrations/2_deploy_contracts.ts:10:18 - error TS2304: Cannot find name 'artifacts'.

10 const Fallback = artifacts.require('Fallback');

migrations/2_deploy_contracts.ts:13:13 - error TS2503: Cannot find namespace 'Truffle'.

13 deployer: Truffle.Deployer,

Any idea on how to define types for artifacts and Truffle, or a workaround to run the migration files with TypeScript successfully?

3
  • This does not solve your problem, but there is another newer smart contract development framework called Hardhat that is TypeScript first: hardhat.org/guides/typescript.html It has 100% feature parity with Truffle and exceeds Truffle in many areas. Mar 14, 2021 at 11:24
  • 1
    Thanks for the suggestion @MikkoOhtamaa. I've actually heard quite a good feedback about this framework, so will have to give a try. Mar 14, 2021 at 12:06
  • @SergiJuanati I want to echo Mikko's suggestion. Hardhat is written in TypeScript, and is meant to be used by TypeScript users. Pro tip: start from my Solidity template, which bundles a lot of useful TypeScript-related utilities, such as TypeChain. Dec 5, 2021 at 12:14

1 Answer 1

2

You need to generate the types using typechain and inject the variable from 2_deploy_contracts.js

npx typechain --target=truffle-v5 'build/contracts/*.json'
module.exports = require("./2_deploy_contracts.ts")(artifacts);

2_deploy_contracts.ts


module.exports = (artifacts: Truffle.Artifacts) => {
  return async (
    deployer: Truffle.Deployer,
    network: string,
    accounts: string[]
  ) => {
    artifacts.require('Fallback');
    deployer.deploy(Fallback);
  }
}

Your Answer

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

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