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?