help with extracting named components from ABI to Typescript type #3624
Unanswered
yulafezmesi
asked this question in
Question
Replies: 1 comment
-
|
you can use the AbiParameterToPrimitiveType utility from viem (or abitype). if your tuple components have names in the ABI, this utility automatically converts them into a TypeScript object instead of an array. you can extract the specific tuple type with its names this way: import { type AbiParameterToPrimitiveType } from 'viem'
// 1. Define your ABI (Must be 'as const' to preserve names!)
const abi = [
{
name: 'createUser',
type: 'function',
inputs: [
{
name: 'user',
type: 'tuple',
components: [ // <--- These names are what we want
{ name: 'username', type: 'string' },
{ name: 'age', type: 'uint256' },
{ name: 'isActive', type: 'bool' }
]
}
],
outputs: []
}
] as const
// 2. Extract the specific Tuple Parameter
// We grab the first input of 'createUser'
type UserTupleParam = typeof abi[0]['inputs'][0]
// 3. Convert to Object Type
// Result: { username: string; age: bigint; isActive: boolean }
type UserObject = AbiParameterToPrimitiveType<UserTupleParam>
// If you just want the keys ('username' | 'age' | 'isActive'):
type UserKeys = keyof UserObject |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone! I'm a bit stuck while working with AbiParametersToPrimitiveTypes from Viem. I'm trying to extract component names from the ABI itself — specifically for tuple arrays — so I can define custom renderers per field name for a particular event.
stackblitz
Here’s the ABI event I’m working with:
using
The resulting structure gives me something like a tuple-array style object:
What I want instead is something where the component names are preserved, like:
Beta Was this translation helpful? Give feedback.
All reactions