113 ethereum hardhat : Using TypeScript
Tags: blockchain, ethereum, hardhat
Published: 2022-07-20 19:33:42

• [introduce](#index1)  
• [Enabling TypeScript support](#index2)  
• [TypeScript configuration](#index3)  
• [Writing tests and scripts in TypeScript](#index4)  
• [Support for path mappings](#index5)  
• [Performance optimizations](#index6)  
• [Running your tests and scripts directly with `ts-node`](#index7)   
• [hardhat Tutorials , hardhat 教程](#index98)   
• [Contact 联系方式](#index99)

# <span id='index1'>• introduce</span>  

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](https://www.typescriptlang.org/).

For a general overview of using Hardhat refer to the [Getting started guide](../getting-started/index.md).

# <span id='index2'>• Enabling TypeScript support</span>  

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
```

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
```

# <span id='index3'>• TypeScript configuration</span>  

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/
```

Then, you should follow the steps mentioned in the [Installing dependencies](#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
```

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`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) 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.

# <span id='index4'>• Writing tests and scripts in TypeScript</span>  

When using JavaScript, all the properties in the [Hardhat Runtime Environment](../advanced/hardhat-runtime-environment.md) 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](../getting-started/index.md) 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`](https://github.com/ethereum-ts/TypeChain/tree/master/packages/hardhat). It generates typing files (`*.d.ts`) based on ABI's, and it requires little to no configuration.

# <span id='index5'>• Support for path mappings</span>  

Typescript allows defining custom [path mappings](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) via the [`paths`](https://www.typescriptlang.org/tsconfig#paths) configuration option:

```
{
  compilerOptions: {
    paths: { "~/*": ["src/*"] },
    // ...Other compilerOptions
  },
}
```

To support this option when running Hardhat tests or scripts, you need to install the package [`tsconfig-paths`](https://www.npmjs.com/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;
```

# <span id='index6'>• Performance optimizations</span>  

Under the hood, Hardhat uses [ts-node](https://www.npmjs.com/package/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`.

# <span id='index7'>• Running your tests and scripts directly with `ts-node`</span>  

When running Hardhat scripts without the CLI, you need to use `ts-node`'s [`--files` flag](https://www.npmjs.com/package/ts-node#help-my-types-are-missing).

This can also be enabled with `TS_NODE_FILES=true`.

[hardhat runtime environment]: ../advanced/hardhat-runtime-environment.md

# <span id='index98'>• hardhat Tutorials , hardhat 教程</span>  
CN 中文 Github  [hardhat 教程 : github.com/565ee/hardhat_CN](https://github.com/565ee/hardhat_CN)  
CN 中文 CSDN    [hardhat 教程 : blog.csdn.net/wx468116118](https://blog.csdn.net/wx468116118/category_11923442.html)  
EN 英文 Github  [hardhat Tutorials : github.com/565ee/hardhat_EN](https://github.com/565ee/hardhat_EN)  

# <span id='index99'>• Contact 联系方式</span>  
Homepage   : [565.ee](https://565.ee)  
GitHub     : [github.com/565ee](https://github.com/565ee)  
Email      : 565.eee@gmail.com  
Facebook   : [facebook.com/565.ee](https://facebook.com/565.ee)  
Twitter    : [twitter.com/565_eee](https://twitter.com/565_eee)  
Telegram   : [t.me/ee_565](https://t.me/ee_565)