Docs
Dev
TypeScript

ts

compile to js with tsc. if no tsc, add globally npm i typescript -g

tsconfig

{
  "compilerOptions": {
    "lib": ["es6"],
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "resolveJsonModule": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "sourceMap": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

Errors

unknown file extension .ts for /app.ts

stack solution (opens in a new tab)

  1. remove "type": "module" from package.json
  2. check "@types/node" and "ts-node" and "typescript" are dependencies in package.json
  3. change node app.ts to ts-node-esm app.ts or
  4. if the project uses .ts imports & requires add this to tsconfig.json
{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

imports

answer (opens in a new tab)

  • js: require("hardhat-gas-reporter")

  • ts: import "hardhat-gas-reporter"

  • js: const { task } = require("hardhat/config")

  • ts: import { task } from "hardhat/config"

  • js: module.exports = {}

  • ts: export default task

  • js

describe("SimpleStorage", function () {
    let simpleStorageFactory, simpleStorage
    beforeEach(async function () {
        simpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
        simpleStorage = await simpleStorageFactory.deploy()
    })
  • ts
import { SimpleStorage__factory, SimpleStorage } from "../typechain-types"
 
describe("SimpleStorage", function () {
    let simpleStorageFactory: SimpleStorage__factory,
        simpleStorage: SimpleStorage
    beforeEach(async function () {
        simpleStorageFactory = (await ethers.getContractFactory(
            "SimpleStorage"
        )) as SimpleStorage__factory
        simpleStorage = await simpleStorageFactory.deploy()
    })
  • why simpleStorageFactory = await.... as SimpleStorage__Factory but not simpleStorage = await...

dealing with nested imports

given this tree structure

📦test ┗ 📜test-deploy.ts
📦typechain-types ┣ 📂factories ┃ ┣ 📜SimpleStorage__factory.ts ┃ ┗ 📜index.ts ┣ 📜SimpleStorage.ts ┣ 📜common.ts ┣ 📜hardhat.d.ts ┗ 📜index.ts

I want import { SimpleStorage__factory, SimpleStorage } from "../typechain-types" in test-deploy.ts

SimpleStorage__factory
  1. in 📦typechain-types > 📂factories > 📜SimpleStorage__factory.ts => export class SimpleStorage__factory
  2. in 📦typechain-types > 📂factories > 📜index.ts => export { SimpleStorage__factory } from "./SimpleStorage__factory";
  3. in 📦typechain-types > 📜index.ts => export * as factories from "./factories"; & export { SimpleStorage__factory } from "./factories/SimpleStorage__factory";
SimpleStorage
  1. in 📦typechain-types > 📜SimpleStorage.ts => export interface SimpleStorage
  2. in 📦typechain-types > 📜index.ts => export type { SimpleStorage } from "./SimpleStorage";

  • import task from "hardhat/config" import

  • import {task} from "hardhat/config" import the task functio

    n