DEV Community

Cover image for 113 ethereum hardhat : Using TypeScript

Posted on

113 ethereum hardhat : Using TypeScript


Enabling TypeScript support

TypeScript configuration

Writing tests and scripts in TypeScript

Support for path mappings

Performance optimizations

Running your tests and scripts directly with ts-node

hardhat Tutorials , hardhat 教程

Contact 联系方式

• introduce

In this guide, we will go through the steps to get a Hardhat project working with TypeScript. This means that you can write your Hardhat config, tasks, scripts and tests in TypeScript.

For a general overview of using Hardhat refer to the Getting started guide.

• Enabling TypeScript support

Hardhat will automatically enable its TypeScript support if your config file ends in .ts and is written in valid TypeScript. This requires a few changes to work properly.

Installing dependencies

Hardhat uses TypeScript and ts-node under the hood, so you need to install them. To do it, open your terminal, go to your Hardhat project, and run:

npm install --save-dev ts-node typescript
Enter fullscreen mode Exit fullscreen mode

To be able to write your tests in TypeScript, you also need these packages:

npm install --save-dev chai @types/node @types/mocha @types/chai
Enter fullscreen mode Exit fullscreen mode

• TypeScript configuration

You can easily turn a JavaScript Hardhat config file into a TypeScript one. Let's see how this is done starting with a fresh Hardhat project.

Open your terminal, go to an empty folder, run npx hardhat, and go through the steps to create a JavaScript project. When you're done your project directory should look something like this:

$ ls -l
total 1200
drwxr-xr-x    3 pato  wheel      96 Oct 20 12:50 contracts/
-rw-r--r--    1 pato  wheel     567 Oct 20 12:50 hardhat.config.js
drwxr-xr-x  434 pato  wheel   13888 Oct 20 12:52 node_modules/
-rw-r--r--    1 pato  wheel  604835 Oct 20 12:52 package-lock.json
-rw-r--r--    1 pato  wheel     460 Oct 20 12:52 package.json
drwxr-xr-x    3 pato  wheel      96 Oct 20 12:50 scripts/
drwxr-xr-x    3 pato  wheel      96 Oct 20 12:50 test/
Enter fullscreen mode Exit fullscreen mode

Then, you should follow the steps mentioned in the Installing dependencies section above.

Now, we are going to rename the config file from hardhat.config.js to hardhat.config.ts, just run:

mv hardhat.config.js hardhat.config.ts
Enter fullscreen mode Exit fullscreen mode

We need to a single change to your config for it to work with TypeScript: you must use import/export instead of require/module.exports.

By using TypeScript, you can also type your configuration, which will save you from typos and other mistakes.

For example, the sample project's config turns from this:

<<< @/../packages/hardhat-core/sample-projects/javascript/hardhat.config.js{1,4}

into this:

<<< @/../packages/hardhat-core/sample-projects/typescript/hardhat.config.ts{1,2,4,8}

Finally, you need to create a tsconfig.json file. Here's our recommended one:

<<< @/../packages/hardhat-core/sample-projects/typescript/tsconfig.json

And that's really all it takes. Now you can write your config, tests, tasks and scripts in TypeScript.

• Writing tests and scripts in TypeScript

When using JavaScript, all the properties in the Hardhat Runtime Environment are injected into the global scope. When using TypeScript nothing will be available in the global scope and you will need to import everything explicitly using, for example, import { ethers } from "hardhat".

Follow the Getting started guide and create a TypeScript project for a complete example on how to write tests and scripts using TypeScript.

If you want Hardhat to generate types for your smart contract you should install and use @typechain/hardhat. It generates typing files (*.d.ts) based on ABI's, and it requires little to no configuration.

• Support for path mappings

Typescript allows defining custom path mappings via the paths configuration option:

  compilerOptions: {
    paths: { "~/*": ["src/*"] },
    // ...Other compilerOptions
Enter fullscreen mode Exit fullscreen mode

To support this option when running Hardhat tests or scripts, you need to install the package tsconfig-paths and register it in your hardhat.config.ts:

import { HardhatUserConfig } from "hardhat/config";

// This adds support for typescript paths mappings
import "tsconfig-paths/register";

const config: HardhatUserConfig = {
  // Your type-safe config goes here

export default config;
Enter fullscreen mode Exit fullscreen mode

• Performance optimizations

Under the hood, Hardhat uses ts-node to support TypeScript. By default, it will recompile and type-check everything on every run. Depending on your project's size, this can get slow.

You can make Hardhat run faster by preventing ts-node from type-checking your project. This is done by setting the TS_NODE_TRANSPILE_ONLY env variable to 1.

For example, you can run your TypeScript-based tests faster like this TS_NODE_TRANSPILE_ONLY=1 npx hardhat test.

• Running your tests and scripts directly with ts-node

When running Hardhat scripts without the CLI, you need to use ts-node's --files flag.

This can also be enabled with TS_NODE_FILES=true.

• hardhat Tutorials , hardhat 教程

CN 中文 Github hardhat 教程 :

CN 中文 CSDN hardhat 教程 :

EN 英文 Github hardhat Tutorials :

• Contact 联系方式

Homepage :

GitHub :

Email :

Facebook :

Twitter :

Telegram :

Top comments (0)