How to solve this type script type check?

Language : typescript

  const [transactions, setTransactions] = useState([]);

  useEffect(() => {
    const fetchTransactions = async () => {
      // ropsten eth rinkeby
      const data = await Web3API.account.getTransactions({
        chain: chainId,
        address: account,
        limit: 5,
      });
      if (data) {
        setTransactions(data.result);
      }
    };
    setTimeout(() => {
      fetchTransactions();
    }, 3000);
  }, [Web3API.account, account, chainId, user]);

I fixed with below method, any guy has better one?

type TranType =
  | {
      hash: string;
      nonce: string;
      transaction_index: string;
      from_address: string;
      to_address: string;
      value: string;
      gas: string;
      gas_price: string;
      input: string;
      receipt_cumulative_gas_used: string;
      receipt_gas_used: string;
      receipt_contract_address: string;
      receipt_root: string;
      receipt_status: string;
      block_timestamp: string;
      block_number: string;
      block_hash: string;
    }[]
  | undefined;

const [transactions, setTransactions] = useState<TranType>();

setTransactions(data.result);

If it works it should be fine. You are just typing the API response directly and it doesnโ€™t look like thereโ€™s one you can import to use.

1 Like