So I am trying to define a type of web3 contract with typescript. We know that a web3 contract is defined in the form that

var myContract = new web3.eth.Contract(contractABI, contractAddress)

My question is that How to define a type of such contract so that the following code is possible

function myFunction(someContract: contract){...}

Any feedback will be appreciated.

  • What would be the intended usage? and why do you want to define a contract like that?
    – Ismael
    Commented Jul 17, 2021 at 5:30
  • I have figured out. It turns out that I can simply import such type from web3 source code.
    – CoolGas
    Commented Jul 17, 2021 at 13:33
  • 1
    I would be nice if you can write what changes were made so the question can be mark as answered.
    – Ismael
    Commented Jul 17, 2021 at 16:29
  • You can use Typechain to generate TypeScript bindings for your Ethereum smart contracts. If you use my Hardhat template, you don't even have to configure Typechain yourself! Commented Oct 3, 2022 at 17:02

2 Answers 2


You can import types from the actual modules instead of 'web3'...

import type {Eth} from 'web3-eth';
import type {Contract} from 'web3-eth-contract';
import type {AbiItem} from 'web3-utils';

const eth: Eth = web3.eth;
const contract: Contract = new web3.eth.Contract(abi as AbiItem[], '...');

Turns out that this can be easily done by import type Contract from the source code of web3.

  • 4
    Can you add a little snippet how the code looks like?
    – Ismael
    Commented Jul 17, 2021 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.