Getting Started
This section will help you start using ABIType in your TypeScript project. You can also try ABIType online in a TypeScript Playground.
Install
pnpm add abitype
pnpm add abitype
bun add abitype
bun add abitype
npm i abitype
npm i abitype
yarn add abitype
yarn add abitype
TypeScript Version
ABIType requires typescript@>=5.0.4
.
Usage
Since ABIs can contain deeply nested arrays and objects, you must either assert ABIs to constants using const
assertions or use the built-in narrow
function (works with JavaScript). This allows TypeScript to take the most specific type for expressions and avoid type widening (e.g. no going from "hello"
to string
).
const erc20Abi = [...] as const
const erc20Abi = <const>[...]
const erc20Abi = [...] as const
const erc20Abi = <const>[...]
import { narrow } from 'abitype'
const erc20Abi = narrow([...])
import { narrow } from 'abitype'
const erc20Abi = narrow([...])
Once your ABIs are set up correctly, you can use the exported types and utilities to work with them. You can also import already set-up ABIs from the abitype/abis
entrypoint to get started quickly.
tsTry
import {ExtractAbiFunctionNames } from 'abitype'import {erc20Abi } from 'abitype/abis'typeResult =ExtractAbiFunctionNames <typeoferc20Abi , 'view'>
tsTry
import {ExtractAbiFunctionNames } from 'abitype'import {erc20Abi } from 'abitype/abis'typeResult =ExtractAbiFunctionNames <typeoferc20Abi , 'view'>
What's next?
After setting up your project with ABIType, you are ready to dive in further! Here are some places to start:
- Learn about the types and utilities available in ABIType.
- Follow along with a walkthrough on building a type-safe
readContract
function. - Check out comparisons between features in ABIType and TypeChain as well as ABIType and ethers.js.
- Make reading and writing ABIs more human with human-readable ABI support.