Web3.js: Typing issue in web3.eth.getBlock

Created on 23 Oct 2019  路  3Comments  路  Source: ChainSafe/web3.js

I guess we need to change the typing for web3.eth.getBlock because when we pass true in web3.eth.getBlock it returns the array of transaction objects instead of an array of transaction strings.

Currently, the type is (property) Block.transactions: Transaction[] | string[].
So, when you will try to use true with getBlock then typescript will give you an error:-

Types of parameters 'tx' and 'value' are incompatible.
      Type 'string' is not assignable to type 'Transaction'.ts(2345)

So, I think we need to create two different types of getBlock like BlockTransactionsObject and BlockTransactionString.

Please suggest better name or a better way to solve this issue.

Version: 1.2.2 as well as 2.x

1.x 2.x types

Most helpful comment

sure but why can you not write code like:

const block = await Web3Service._instance.eth.getBlock(1234, true);
const transactions = block.transactions as Transaction[];

// do what you want with the transactions

i don't get any TS errors doing it like this with strict mode on.

we should probably yes create 2 different interfaces on that case which returns only Transaction[] but you can work around with the code to make it not error.

if you're getting errors with the above can you show me your snippet and i can take a look.

All 3 comments

sure but why can you not write code like:

const block = await Web3Service._instance.eth.getBlock(1234, true);
const transactions = block.transactions as Transaction[];

// do what you want with the transactions

i don't get any TS errors doing it like this with strict mode on.

we should probably yes create 2 different interfaces on that case which returns only Transaction[] but you can work around with the code to make it not error.

if you're getting errors with the above can you show me your snippet and i can take a look.

@joshstevens19 Yes. The above code is proper and it will work. It's a workaround to not get error. But, If some will not write const transactions = block.transactions as Transaction[] then, he will get an error.

Consider this code:-

this.web3.eth.getBlock(i, true)
.then((block) => {
    block.transactions.forEach((tx: Transaction) => {
    });
});

Ideally, if we will make two different interfaces then, it will work fine. Something like below code.

getBlock(blockHashOrBlockNumber: string | number): Promise<BlockTransactionString>;
getBlock(
    blockHashOrBlockNumber: string | number,
    returnTransactionObjects: boolean
): Promise<BlockTransactionString>;
getBlock(
    blockHashOrBlockNumber: string | number,
    callback?: (error: Error, block: BlockTransactionString) => void
): Promise<BlockTransactionString>;
getBlock(
    blockHashOrBlockNumber: string | number,
    returnTransactionObjects: boolean,
    callback?: (error: Error, block: BlockTransactionObject) => void
): Promise<BlockTransactionObject>;


export interface BlockTransactionObject extends BlockHeader {
    transactions: Transaction[];
    size: number;
    difficulty: number;
    totalDifficulty: number;
    uncles: string[];
}

export interface BlockTransactionString extends BlockHeader {
    transactions: string[];
    size: number;
    difficulty: number;
    totalDifficulty: number;
    uncles: string[];
}

we should probably yes create 2 different interfaces on that case which returns only Transaction[] but you can work around with the code to make it not error.

Yes, it will be better.

yep that way would be better for sure i am away till Monday can put in a fix when i'm back. :+1: thanks for raising.

Was this page helpful?
0 / 5 - 0 ratings