Skip to content

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

bash
pnpm add abitype
pnpm add abitype
bash
bun add abitype
bun add abitype
bash
npm i abitype
npm i abitype
bash
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).

ts
const erc20Abi = [...] as const
const erc20Abi = <const>[...]
const erc20Abi = [...] as const
const erc20Abi = <const>[...]
ts
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.

ts
ts
import { ExtractAbiFunctionNames } from 'abitype'
import { erc20Abi } from 'abitype/abis'
 
type Result = ExtractAbiFunctionNames<typeof erc20Abi, 'view'>
type Result = "symbol" | "name" | "allowance" | "balanceOf" | "decimals" | "totalSupply"
Try
ts
import { ExtractAbiFunctionNames } from 'abitype'
import { erc20Abi } from 'abitype/abis'
 
type Result = ExtractAbiFunctionNames<typeof erc20Abi, 'view'>
type Result = "symbol" | "name" | "allowance" | "balanceOf" | "decimals" | "totalSupply"
Try

What's next?

After setting up your project with ABIType, you are ready to dive in further! Here are some places to start:

Released under the MIT License.