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

1) My current setup:


    "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"],


  files: true,

2) Migration files:


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

module.exports = function (deployer) {


module.exports = require("./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?

  • 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 '21 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 '21 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 '21 at 12:14

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);


module.exports = (artifacts: Truffle.Artifacts) => {
  return async (
    deployer: Truffle.Deployer,
    network: string,
    accounts: string[]
  ) => {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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