Replies: 3 comments 1 reply
-
The result type is a normal array, but also includes names properties to access values, for convenience. So, you can use The Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
For the moment, I'm using this as a workaround. // convertToStruct takes an array type eg. Inventory.ItemStructOutput and converts it to an object type.
export const convertToStruct = <A extends Array<unknown>>(arr: A): ExtractPropsFromArray<A> => {
const keys = Object.keys(arr).filter((key) => isNaN(Number(key)));
const result = {};
// @ts-ignore
arr.forEach((item, index) => (result[keys[index]] = item));
return result as A;
};
// This is to remove unnecessary properties from the output type. Use it eg. `ExtractPropsFromArray<Inventory.ItemStructOutput>`
export type ExtractPropsFromArray<T> = Omit<T, keyof Array<unknown> | `${number}`>; |
Beta Was this translation helpful? Give feedback.
-
I've written this workaround. It returns an object. export function fromStructToObject<T extends object>(struct : [] & T) : T{
struct = {...struct};
const keysNumber = Object.keys(struct).length;
for(var i = 0; i < keysNumber/2; i++){
delete struct[i];
}
return struct;
} Then you could do something like this to get the array of the struct Object.values(fromStructToObject(struct)) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Searching the docs, this utility doesn't seem to exist.
I am returning the array of structs from my contract. Used hardhat to compile contract, types and for a deployment.
I am unsure if this is how it's returned in other web3 libs too or it's specific to ethers?
Here's the part of the actual response that I receive:
The actual struct type in solidity is:
Expected response?
Am I missing something? Is there an util from ethers that can format this to expectation? Optionally the ID should become a number as well, not object that needs util format.
Beta Was this translation helpful? Give feedback.
All reactions