How can you import Viem-compatible contract types directly #4754
Replies: 2 comments
-
The type lives in the This workaround seems to work, but typescript won't compile until you compile your contracts. But at least it doesn't need you to use explicit paths: import { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";
import { ArtifactsMap } from "hardhat/types/artifacts";
type TestToken = GetContractReturnType<ArtifactsMap["TestToken"]["abi"]> A more involved alternative, but that will work even if you contracts are not compiled, is this: import { GetContractReturnType } from "@nomicfoundation/hardhat-viem/types";
import { ArtifactsMap } from "hardhat/types/artifacts";
type GetContractType<T extends string> = ArtifactsMap extends {[key in T]: any} ? GetContractReturnType<ArtifactsMap[T]["abi"]> : GetContractReturnType;
type TestToken = GetContractType<"TestToken"> I haven't tested this in depth, so please let me know if it works or doesn't! |
Beta Was this translation helpful? Give feedback.
-
You mentioned but I made an update to the It's not as comprehensive as the stuff that |
Beta Was this translation helpful? Give feedback.
-
I was going to raise this as an issue, but I think it's just my not being able to find where to import contract instance types, since this issue is already resolved.
Basically I just want to use the type of a contract that is deployed with something like:
Right now I have created a declaration file like the below, but it will get cumbersome to update/manage as the number of contract types grows. Not to mention that it requires hardhat artifacts to be created, which intellisense, linting, etc. won't work until the contracts are compiled.
Is there any single place where I can just import the type?
Beta Was this translation helpful? Give feedback.
All reactions