0

I'm working on a Hardhat project with TypeScript enabled. Currently, I can run my TypeScript scripts even if there are type errors, presumably due to the use of ts-node. I would like to enforce type correctness before my scripts actually execute. How can I achieve this?

1 Answer 1

0

In Hardhat projects that utilize TypeScript, by default, TypeScript files are transpiled on-the-fly to JavaScript using ts-node and then executed. This means even if there are type errors, the scripts might still run. However, for those wanting to ensure strict type correctness before any script or task execution, Hardhat offers a straightforward solution.

hardhat provides the --typecheck flag as detailed in the official Hardhat documentation. When you append this flag to your Hardhat commands, the system will enforce type-checking and won't proceed with the execution if type errors are present.

For instance, running:

npx hardhat test

will execute your tests even if there's a TypeScript compilation error. However, if you run:

npx hardhat test --typecheck

hardhat via ts-node will check for TypeScript errors first, and if any are detected, it will halt execution and report the errors.

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.