Skip to content

Utilities

Utility types for working with ABIs and EIP-712 Typed Data.

AbiParameterToPrimitiveType

Converts AbiParameter to corresponding TypeScript primitive type.

NameDescriptionType
abiParameterParameter to convert to TypeScript representation.AbiParameter
abiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive type.type (inferred)

Example

import { 
type AbiParameterToPrimitiveType<abiParameter extends AbiParameter | { name: string; type: unknown; }, abiParameterKind extends AbiParameterKind = AbiParameterKind> = abiParameter["type"] extends AbiBasicType ? AbiTypeToPrimitiveType<abiParameter["type"], abiParameterKind> : abiParameter extends { ...; } ? AbiComponentsToPrimitiveType<...> : MaybeExtractArrayParameterType<...> extends [...] ? AbiArrayToPrimitiveType<...> : abiParameter extends { ...; } ? abiParameter["components"] : unknown

Converts AbiParameter to corresponding TypeScript primitive type.

AbiParameterToPrimitiveType
} from 'abitype'
type
type Result = `0x${string}`
Result
=
type AbiParameterToPrimitiveType<abiParameter extends AbiParameter | { name: string; type: unknown; }, abiParameterKind extends AbiParameterKind = AbiParameterKind> = abiParameter["type"] extends AbiBasicType ? AbiTypeToPrimitiveType<abiParameter["type"], abiParameterKind> : abiParameter extends { ...; } ? AbiComponentsToPrimitiveType<...> : MaybeExtractArrayParameterType<...> extends [...] ? AbiArrayToPrimitiveType<...> : abiParameter extends { ...; } ? abiParameter["components"] : unknown

Converts AbiParameter to corresponding TypeScript primitive type.

AbiParameterToPrimitiveType
<{
name: "owner"
name
: 'owner'
type: "address"
type
: 'address'
}>

AbiParametersToPrimitiveTypes

Converts array of AbiParameter to corresponding TypeScript primitive types.

NameDescriptionType
abiParametersParameters to convert to TypeScript representations.readonly AbiParameter[]
abiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive types.type[] (inferred)

Example

import { 
type AbiParametersToPrimitiveTypes<abiParameters extends readonly AbiParameter[], abiParameterKind extends AbiParameterKind = AbiParameterKind> = { [key in keyof { [key in keyof abiParameters]: AbiParameterToPrimitiveType<abiParameters[key], abiParameterKind>; }]: { [key in keyof abiParameters]: AbiParameterToPrimitiveType<abiParameters[key], abiParameterKind>; }[key]; }

Converts array of AbiParameter to corresponding TypeScript primitive types.

AbiParametersToPrimitiveTypes
} from 'abitype'
type
type Result = [`0x${string}`, bigint]
Result
=
type AbiParametersToPrimitiveTypes<abiParameters extends readonly AbiParameter[], abiParameterKind extends AbiParameterKind = AbiParameterKind> = { [key in keyof { [key in keyof abiParameters]: AbiParameterToPrimitiveType<abiParameters[key], abiParameterKind>; }]: { [key in keyof abiParameters]: AbiParameterToPrimitiveType<abiParameters[key], abiParameterKind>; }[key]; }

Converts array of AbiParameter to corresponding TypeScript primitive types.

AbiParametersToPrimitiveTypes
<
[ {
name: "to"
name
: 'to';
type: "address"
type
: 'address'; },
{
name: "tokenId"
name
: 'tokenId';
type: "uint256"
type
: 'uint256'; },
] >

AbiTypeToPrimitiveType

Converts AbiType to corresponding TypeScript primitive type.

NameDescriptionType
abiTypeABI type to convert to TypeScript representation.AbiType
abiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive type.type (inferred)

Example

import { 
type AbiTypeToPrimitiveType<abiType extends AbiType, abiParameterKind extends AbiParameterKind = AbiParameterKind> = abiType extends "bytes" | "bytes1" | "bytes2" | "bytes3" | "bytes4" | "bytes5" | "bytes6" | "bytes7" | "bytes8" | "bytes9" | "bytes10" | "bytes11" | "bytes12" | ... 19 more ... | "bytes32" ? PrimitiveTypeLookup[abiType][abiParameterKind] : PrimitiveTypeLookup[abiType]

Converts AbiType to corresponding TypeScript primitive type.

Does not include full array or tuple conversion. Use AbiParameterToPrimitiveType to fully convert arrays and tuples.

AbiTypeToPrimitiveType
} from 'abitype'
type
type Result = `0x${string}`
Result
=
type AbiTypeToPrimitiveType<abiType extends AbiType, abiParameterKind extends AbiParameterKind = AbiParameterKind> = abiType extends "bytes" | "bytes1" | "bytes2" | "bytes3" | "bytes4" | "bytes5" | "bytes6" | "bytes7" | "bytes8" | "bytes9" | "bytes10" | "bytes11" | "bytes12" | ... 19 more ... | "bytes32" ? PrimitiveTypeLookup[abiType][abiParameterKind] : PrimitiveTypeLookup[abiType]

Converts AbiType to corresponding TypeScript primitive type.

Does not include full array or tuple conversion. Use AbiParameterToPrimitiveType to fully convert arrays and tuples.

AbiTypeToPrimitiveType
<'address'>

ExtractAbiError

Extracts AbiError with name from Abi.

NameDescriptionType
abiABI.Abi
errorNameName of error.string (inferred)
returnsABI Error.AbiError

Example

import { 
type ExtractAbiError<abi extends Abi, errorName extends ExtractAbiErrorNames<abi>> = Extract<abi[number], { type: "error"; }> extends { name: errorName; } ? { name: errorName; } & Extract<abi[number], { type: "error"; }> : never

Extracts AbiError with name from Abi.

ExtractAbiError
} from 'abitype'
const
const abi: readonly [{ readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
= [
{
name: "BarError"
name
: 'BarError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
{
name: "FooError"
name
: 'FooError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
] as
type const = readonly [{ readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }]
const
type
type Result = { readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }
Result
=
type ExtractAbiError<abi extends Abi, errorName extends ExtractAbiErrorNames<abi>> = Extract<abi[number], { type: "error"; }> extends { name: errorName; } ? { name: errorName; } & Extract<abi[number], { type: "error"; }> : never

Extracts AbiError with name from Abi.

ExtractAbiError
<typeof
const abi: readonly [{ readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
, 'FooError'>

ExtractAbiErrorNames

Extracts all AbiError names from Abi.

NameDescriptionType
abiABI.Abi
returnsABI Error names.string (inferred)

Example

import { 
type ExtractAbiErrorNames<abi extends Abi> = Extract<abi[number], { type: "error"; }>["name"]

Extracts all AbiError names from Abi.

ExtractAbiErrorNames
} from 'abitype'
const
const abi: readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
= [
{
name: "FooError"
name
: 'FooError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
{
name: "BarError"
name
: 'BarError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
] as
type const = readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
const
type
type Result = "FooError" | "BarError"
Result
=
type ExtractAbiErrorNames<abi extends Abi> = Extract<abi[number], { type: "error"; }>["name"]

Extracts all AbiError names from Abi.

ExtractAbiErrorNames
<typeof
const abi: readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
>

ExtractAbiErrors

Extracts all AbiError types from Abi.

NameDescriptionType
abiABI.Abi
returnsABI Errors.AbiError (union)

Example

import { 
type ExtractAbiErrors<abi extends Abi> = abi[number] extends { type: "error"; } ? abi[number] : never

Extracts all AbiError types from Abi.

ExtractAbiErrors
} from 'abitype'
const
const abi: readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
= [
{
name: "FooError"
name
: 'FooError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
{
name: "BarError"
name
: 'BarError',
type: "error"
type
: 'error',
inputs: readonly []
inputs
: [] },
] as
type const = readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
const
type
type Result = { readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; } | { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }
Result
=
type ExtractAbiErrors<abi extends Abi> = abi[number] extends { type: "error"; } ? abi[number] : never

Extracts all AbiError types from Abi.

ExtractAbiErrors
<typeof
const abi: readonly [{ readonly name: "FooError"; readonly type: "error"; readonly inputs: readonly []; }, { readonly name: "BarError"; readonly type: "error"; readonly inputs: readonly []; }]
abi
>

ExtractAbiEvent

Extracts AbiEvent with name from Abi.

NameDescriptionType
abiABI.Abi
eventNameName of event.string (inferred)
returnsABI Event.AbiEvent

Example

import { 
type ExtractAbiEvent<abi extends Abi, eventName extends ExtractAbiEventNames<abi>> = Extract<abi[number], { type: "event"; }> extends { name: eventName; } ? { name: eventName; } & Extract<abi[number], { type: "event"; }> : never

Extracts AbiEvent with name from Abi.

ExtractAbiEvent
} from 'abitype'
const
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
= [
{
name: "Approval"
name
: 'Approval',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "approved"
name
: 'approved',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, {
name: "Transfer"
name
: 'Transfer',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, ] as
type const = readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
const
type
type Result = { readonly name: "Transfer"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }
Result
=
type ExtractAbiEvent<abi extends Abi, eventName extends ExtractAbiEventNames<abi>> = Extract<abi[number], { type: "event"; }> extends { name: eventName; } ? { name: eventName; } & Extract<abi[number], { type: "event"; }> : never

Extracts AbiEvent with name from Abi.

ExtractAbiEvent
<typeof
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
, 'Transfer'>

ExtractAbiEventNames

Extracts all AbiEvent names from Abi.

NameDescriptionType
abiABI.Abi
returnsABI Error names.string (inferred)

Example

import { 
type ExtractAbiEventNames<abi extends Abi> = Extract<abi[number], { type: "event"; }>["name"]

Extracts all AbiEvent names from Abi.

ExtractAbiEventNames
} from 'abitype'
const
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
= [
{
name: "Approval"
name
: 'Approval',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "approved"
name
: 'approved',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, {
name: "Transfer"
name
: 'Transfer',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, ] as
type const = readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
const
type
type Result = "Approval" | "Transfer"
Result
=
type ExtractAbiEventNames<abi extends Abi> = Extract<abi[number], { type: "event"; }>["name"]

Extracts all AbiEvent names from Abi.

ExtractAbiEventNames
<typeof
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
>

ExtractAbiEvents

Extracts all AbiEvent types from Abi.

NameDescriptionType
abiABI.Abi
returnsABI Events.AbiEvent (union)

Example

import { 
type ExtractAbiEvents<abi extends Abi> = abi[number] extends { type: "event"; } ? abi[number] : never

Extracts all AbiEvent types from Abi.

ExtractAbiEvents
} from 'abitype'
const
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
= [
{
name: "Approval"
name
: 'Approval',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "approved"
name
: 'approved',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, {
name: "Transfer"
name
: 'Transfer',
type: "event"
type
: 'event',
anonymous: false
anonymous
: false,
inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { readonly name: "tokenId"; readonly type: "uint256"; readonly indexed: true; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address',
indexed: true
indexed
: true },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256',
indexed: true
indexed
: true },
], }, ] as
type const = readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
const
type
type Result = { readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; } | { ...; }
Result
=
type ExtractAbiEvents<abi extends Abi> = abi[number] extends { type: "event"; } ? abi[number] : never

Extracts all AbiEvent types from Abi.

ExtractAbiEvents
<typeof
const abi: readonly [{ readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }, { ...; }]
abi
>

ExtractAbiFunction

Extracts AbiFunction with name from Abi.

NameDescriptionType
abiABI.Abi
functionNameName of function.string (inferred)
abiStateMutabilityABI state mutability.AbiStateMutability (optional)
returnsABI Function.AbiFunction

Example

import { 
type ExtractAbiFunction<abi extends Abi, functionName extends ExtractAbiFunctionNames<abi>, abiStateMutability extends AbiStateMutability = AbiStateMutability> = Extract<abi[number], { type: "function"; stateMutability: abiStateMutability; }> extends { name: functionName; } ? { name: functionName; } & Extract<...> : never

Extracts AbiFunction with name from Abi.

ExtractAbiFunction
} from 'abitype'
const
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
= [
{
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]
inputs
: [{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address' }],
outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]
outputs
: [{
name: "balance"
name
: 'balance',
type: "uint256"
type
: 'uint256' }],
}, {
name: "safeTransferFrom"
name
: 'safeTransferFrom',
type: "function"
type
: 'function',
stateMutability: "nonpayable"
stateMutability
: 'nonpayable',
inputs: readonly [{ readonly name: "from"; readonly type: "address"; }, { readonly name: "to"; readonly type: "address"; }, { readonly name: "tokenId"; readonly type: "uint256"; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address' },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address' },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256' },
],
outputs: readonly []
outputs
: [],
}, ] as
type const = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
const
type
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }
Result
=
type ExtractAbiFunction<abi extends Abi, functionName extends ExtractAbiFunctionNames<abi>, abiStateMutability extends AbiStateMutability = AbiStateMutability> = Extract<abi[number], { type: "function"; stateMutability: abiStateMutability; }> extends { name: functionName; } ? { name: functionName; } & Extract<...> : never

Extracts AbiFunction with name from Abi.

ExtractAbiFunction
<typeof
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
, 'balanceOf'>

ExtractAbiFunctionNames

Extracts all AbiFunction names from Abi.

NameDescriptionType
abiABI.Abi
abiStateMutabilityABI state mutability.AbiStateMutability (optional)
returnsABI Event names.string (inferred)

Example

import { 
type ExtractAbiFunctionNames<abi extends Abi, abiStateMutability extends AbiStateMutability = AbiStateMutability> = Extract<abi[number], { type: "function"; stateMutability: abiStateMutability; }>["name"]

Extracts all AbiFunction names from Abi.

ExtractAbiFunctionNames
} from 'abitype'
const
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
= [
{
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]
inputs
: [{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address' }],
outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]
outputs
: [{
name: "balance"
name
: 'balance',
type: "uint256"
type
: 'uint256' }],
}, {
name: "safeTransferFrom"
name
: 'safeTransferFrom',
type: "function"
type
: 'function',
stateMutability: "nonpayable"
stateMutability
: 'nonpayable',
inputs: readonly [{ readonly name: "from"; readonly type: "address"; }, { readonly name: "to"; readonly type: "address"; }, { readonly name: "tokenId"; readonly type: "uint256"; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address' },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address' },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256' },
],
outputs: readonly []
outputs
: [],
}, ] as
type const = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
const
type
type Result = "balanceOf" | "safeTransferFrom"
Result
=
type ExtractAbiFunctionNames<abi extends Abi, abiStateMutability extends AbiStateMutability = AbiStateMutability> = Extract<abi[number], { type: "function"; stateMutability: abiStateMutability; }>["name"]

Extracts all AbiFunction names from Abi.

ExtractAbiFunctionNames
<typeof
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
>

ExtractAbiFunctions

Extracts all AbiFunction types from Abi.

NameDescriptionType
abiABI.Abi
returnsABI Functions.AbiFunction (union)

Example

import { 
type ExtractAbiFunctions<abi extends Abi, abiStateMutability extends AbiStateMutability = AbiStateMutability> = abi[number] extends { type: "function"; stateMutability: abiStateMutability; } ? abi[number] : never

Extracts all AbiFunction types from Abi.

ExtractAbiFunctions
} from 'abitype'
const
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
= [
{
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]
inputs
: [{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address' }],
outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]
outputs
: [{
name: "balance"
name
: 'balance',
type: "uint256"
type
: 'uint256' }],
}, {
name: "safeTransferFrom"
name
: 'safeTransferFrom',
type: "function"
type
: 'function',
stateMutability: "nonpayable"
stateMutability
: 'nonpayable',
inputs: readonly [{ readonly name: "from"; readonly type: "address"; }, { readonly name: "to"; readonly type: "address"; }, { readonly name: "tokenId"; readonly type: "uint256"; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address' },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address' },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256' },
],
outputs: readonly []
outputs
: [],
}, ] as
type const = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
const
type
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; } | { ...; }
Result
=
type ExtractAbiFunctions<abi extends Abi, abiStateMutability extends AbiStateMutability = AbiStateMutability> = abi[number] extends { type: "function"; stateMutability: abiStateMutability; } ? abi[number] : never

Extracts all AbiFunction types from Abi.

ExtractAbiFunctions
<typeof
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
>

By default, extracts all functions, but you can also filter by AbiStateMutability:

type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>

IsAbi

Checks if type is Abi.

NameDescriptionType
abiABI.Abi
returnsBoolean value. true if valid Abi, false if not.boolean

Example

import { 
type IsAbi<abi> = abi extends Abi ? true : false

Checks if type is Abi.

IsAbi
} from 'abitype'
const
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
= [
{
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]
inputs
: [{
name: "owner"
name
: 'owner',
type: "address"
type
: 'address' }],
outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]
outputs
: [{
name: "balance"
name
: 'balance',
type: "uint256"
type
: 'uint256' }],
}, {
name: "safeTransferFrom"
name
: 'safeTransferFrom',
type: "function"
type
: 'function',
stateMutability: "nonpayable"
stateMutability
: 'nonpayable',
inputs: readonly [{ readonly name: "from"; readonly type: "address"; }, { readonly name: "to"; readonly type: "address"; }, { readonly name: "tokenId"; readonly type: "uint256"; }]
inputs
: [
{
name: "from"
name
: 'from',
type: "address"
type
: 'address' },
{
name: "to"
name
: 'to',
type: "address"
type
: 'address' },
{
name: "tokenId"
name
: 'tokenId',
type: "uint256"
type
: 'uint256' },
],
outputs: readonly []
outputs
: [],
}, ] as
type const = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
const
type
type Result = true
Result
=
type IsAbi<abi> = abi extends Abi ? true : false

Checks if type is Abi.

IsAbi
<typeof
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }, { ...; }]
abi
>

IsTypedData

Checks if type is TypedData.

NameDescriptionType
typedDataEIP-712 Typed Data schema.TypedData
returnsBoolean value. true if valid TypedData, false if not.boolean

Example

import { 
type IsTypedData<typedData> = typedData extends { [x: string]: readonly TypedDataParameter[]; [x: `string[${string}]`]: undefined; [x: `function[${string}]`]: undefined; [x: `address[${string}]`]: undefined; [x: `bool[${string}]`]: undefined; [x: `bytes[${string}]`]: undefined; [x: `bytes2[${string}]`]: undefined; [x: `bytes3[${string}]`]: undefined; [x: `bytes1[${string}]`]: undefined; [x: `bytes4[${string}]`]: undefined; [x: `bytes5[${string}]`]: undefined; [x: `bytes6[${string}]`]: undefined; [x: `bytes7[${string}]`]: undefined; [x: `bytes8[${string}]`]: undefined; [x: `bytes9[${string}]`]: undefined; [x: `bytes10[${string}]`]: undefined; [x: `bytes11[${string}]`]: undefined; [x: `bytes12[${string}]`]: undefined; [x: `bytes13[${string}]`]: undefined; [x: `bytes14[${string}]`]: undefined; [x: `bytes15[${string}]`]: undefined; [x: `bytes16[${string}]`]: undefined; [x: `bytes17[${string}]`]: undefined; [x: `bytes18[${string}]`]: undefined; [x: `bytes19[${string}]`]: undefined; [x: `bytes20[${string}]`]: undefined; [x: `bytes21[${string}]`]: undefined; [x: `bytes22[${string}]`]: undefined; [x: `bytes23[${string}]`]: undefined; [x: `bytes24[${string}]`]: undefined; [x: `bytes25[${string}]`]: undefined; [x: `bytes26[${string}]`]: undefined; [x: `bytes27[${string}]`]: undefined; [x: `bytes28[${string}]`]: undefined; [x: `bytes29[${string}]`]: undefined; [x: `bytes30[${string}]`]: undefined; [x: `bytes31[${string}]`]: undefined; [x: `bytes32[${string}]`]: undefined ...

Checks if type is TypedData.

IsTypedData
} from 'abitype'
const
const types: { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
types
= {
type Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]
Person
: [
{
name: "name"
name
: 'name',
type: "string"
type
: 'string' },
{
name: "wallet"
name
: 'wallet',
type: "address"
type
: 'address' },
],
type Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { readonly name: "to"; readonly type: "Person"; }, { readonly name: "contents"; readonly type: "string"; }]
Mail
: [
{
name: "from"
name
: 'from',
type: "Person"
type
: 'Person' },
{
name: "to"
name
: 'to',
type: "Person"
type
: 'Person' },
{
name: "contents"
name
: 'contents',
type: "string"
type
: 'string' },
], } as
type const = { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
const
type
type Result = true
Result
=
type IsTypedData<typedData> = typedData extends { [x: string]: readonly TypedDataParameter[]; [x: `string[${string}]`]: undefined; [x: `function[${string}]`]: undefined; [x: `address[${string}]`]: undefined; [x: `bool[${string}]`]: undefined; [x: `bytes[${string}]`]: undefined; [x: `bytes2[${string}]`]: undefined; [x: `bytes3[${string}]`]: undefined; [x: `bytes1[${string}]`]: undefined; [x: `bytes4[${string}]`]: undefined; [x: `bytes5[${string}]`]: undefined; [x: `bytes6[${string}]`]: undefined; [x: `bytes7[${string}]`]: undefined; [x: `bytes8[${string}]`]: undefined; [x: `bytes9[${string}]`]: undefined; [x: `bytes10[${string}]`]: undefined; [x: `bytes11[${string}]`]: undefined; [x: `bytes12[${string}]`]: undefined; [x: `bytes13[${string}]`]: undefined; [x: `bytes14[${string}]`]: undefined; [x: `bytes15[${string}]`]: undefined; [x: `bytes16[${string}]`]: undefined; [x: `bytes17[${string}]`]: undefined; [x: `bytes18[${string}]`]: undefined; [x: `bytes19[${string}]`]: undefined; [x: `bytes20[${string}]`]: undefined; [x: `bytes21[${string}]`]: undefined; [x: `bytes22[${string}]`]: undefined; [x: `bytes23[${string}]`]: undefined; [x: `bytes24[${string}]`]: undefined; [x: `bytes25[${string}]`]: undefined; [x: `bytes26[${string}]`]: undefined; [x: `bytes27[${string}]`]: undefined; [x: `bytes28[${string}]`]: undefined; [x: `bytes29[${string}]`]: undefined; [x: `bytes30[${string}]`]: undefined; [x: `bytes31[${string}]`]: undefined; [x: `bytes32[${string}]`]: undefined ...

Checks if type is TypedData.

IsTypedData
<typeof
const types: { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
types
>

TypedDataToPrimitiveTypes

Converts EIP-712 TypedData to corresponding TypeScript primitive type.

NameDescriptionType
typedDataEIP-712 Typed Data schema.TypedData
returnsTypeScript representation of schema.{ [name: string]: type } (inferred)

Example

import { 
type TypedDataToPrimitiveTypes<typedData extends TypedData, abiParameterKind extends AbiParameterKind = AbiParameterKind, keyReferences extends { [_: string]: unknown; } | unknown = unknown> = { [key in keyof typedData]: { [key2 in typedData[key][number] as key2["name"]]: key2["type"] extends key ? Error<`Cannot convert self-referencing struct '${key2["type"]}' to primitive type.`> : key2["type"] extends keyof typedData ? key2["type"] extends keyof keyReferences ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 1 more ... | keyof keyReferences ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 3 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 5 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 7 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 9 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends ...

Converts typedData to corresponding TypeScript primitive types.

TypedDataToPrimitiveTypes
} from 'abitype'
const
const types: { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
types
= {
type Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]
Person
: [
{
name: "name"
name
: 'name',
type: "string"
type
: 'string' },
{
name: "wallet"
name
: 'wallet',
type: "address"
type
: 'address' },
],
type Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { readonly name: "to"; readonly type: "Person"; }, { readonly name: "contents"; readonly type: "string"; }]
Mail
: [
{
name: "from"
name
: 'from',
type: "Person"
type
: 'Person' },
{
name: "to"
name
: 'to',
type: "Person"
type
: 'Person' },
{
name: "contents"
name
: 'contents',
type: "string"
type
: 'string' },
], } as
type const = { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
const
type
type Result = { readonly Person: { name: string; wallet: `0x${string}`; }; readonly Mail: { from: { name: string; wallet: `0x${string}`; }; to: { name: string; wallet: `0x${string}`; }; contents: string; }; }
Result
=
type TypedDataToPrimitiveTypes<typedData extends TypedData, abiParameterKind extends AbiParameterKind = AbiParameterKind, keyReferences extends { [_: string]: unknown; } | unknown = unknown> = { [key in keyof typedData]: { [key2 in typedData[key][number] as key2["name"]]: key2["type"] extends key ? Error<`Cannot convert self-referencing struct '${key2["type"]}' to primitive type.`> : key2["type"] extends keyof typedData ? key2["type"] extends keyof keyReferences ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 1 more ... | keyof keyReferences ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 3 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 5 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 7 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends key ? Error<...> : key2["type"] extends keyof Exclude<...> ? key2["type"] extends key | ... 9 more ... | key2["type"] ? Error<...> : { [key in keyof Exclude<...>]: { [key2 in Exclude<...>[key][number] as key2["name"]]: key2["type"] extends ...

Converts typedData to corresponding TypeScript primitive types.

TypedDataToPrimitiveTypes
<typeof
const types: { readonly Person: readonly [{ readonly name: "name"; readonly type: "string"; }, { readonly name: "wallet"; readonly type: "address"; }]; readonly Mail: readonly [{ readonly name: "from"; readonly type: "Person"; }, { ...; }, { ...; }]; }
types
>