ABIType: Ethereum ABI TypeScript Types

1.2.4 · active · verified Tue Apr 21

ABIType is a TypeScript-first library providing strict type definitions and utilities for Ethereum ABIs (Application Binary Interfaces) and EIP-712 Typed Data. It enables developers to achieve compile-time type-checking and inference for contract interactions without requiring external code generation tools. The current stable version is 1.2.4, with frequent patch and minor releases addressing bug fixes, performance improvements, and adding experimental features like named tuple support. Its primary differentiator is its deep integration with TypeScript's type system to convert ABI types (e.g., 'string') directly into TypeScript types (e.g., `string` or `0x${string}`) for enhanced developer experience, widely adopted by prominent libraries such as Wagmi and Viem. It also offers runtime validation capabilities for ABIs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to extract function names, derive primitive types from ABI parameters, and parse custom ABI strings at runtime for type-safe contract interactions.

import type { AbiParametersToPrimitiveTypes, ExtractAbiFunction, ExtractAbiFunctionNames } from 'abitype';
import { erc20Abi, parseAbi } from 'abitype/abis';

// Example 1: Extracting function names from an ABI
type ViewFunctionNames = ExtractAbiFunctionNames<typeof erc20Abi, 'view'>;
//   ^? type ViewFunctionNames = "symbol" | "name" | "allowance" | "balanceOf" | "decimals" | "totalSupply"

// Example 2: Getting primitive types for a function's inputs
type TransferInputTypes = AbiParametersToPrimitiveTypes<
  ExtractAbiFunction<typeof erc20Abi, 'transfer'>['inputs']
>;
//   ^? type TransferInputTypes = readonly [`0x${string}`, bigint]

// Example 3: Parsing a custom ABI string at runtime
const customAbi = parseAbi([
  'function deposit() payable',
  'event Deposit(address indexed user, uint256 amount)'
]);

console.log(customAbi[0].name); // 'deposit'
console.log(customAbi[1].type); // 'event'

// Example 4: Type-checking a variable against an ABI type
type MyCustomAbi = typeof customAbi;

// This will error if the structure doesn't match MyCustomAbi
const invalidAbi: MyCustomAbi = [
  {
    type: 'function',
    name: 'withdraw',
    inputs: [],
    stateMutability: 'nonpayable',
    outputs: []
  }
];

view raw JSON →