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)
- remove
"type": "module"
from package.json - check
"@types/node"
and"ts-node"
and"typescript"
are dependencies in package.json - change
node app.ts
tots-node-esm app.ts
or - if the project uses .ts imports & requires add this to tsconfig.json
{
"compilerOptions": {
"esModuleInterop": true
}
}
imports
-
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 notsimpleStorage = 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
- in
📦typechain-types > 📂factories > 📜SimpleStorage__factory.ts
=>export class SimpleStorage__factory
- in
📦typechain-types > 📂factories > 📜index.ts
=>export { SimpleStorage__factory } from "./SimpleStorage__factory";
- in
📦typechain-types > 📜index.ts
=>export * as factories from "./factories";
&export { SimpleStorage__factory } from "./factories/SimpleStorage__factory";
SimpleStorage
- in
📦typechain-types > 📜SimpleStorage.ts
=>export interface SimpleStorage
- in
📦typechain-types > 📜index.ts
=>export type { SimpleStorage } from "./SimpleStorage";
-
import
task from "hardhat/config"
import -
import
{task} from "hardhat/config"
import the task function