Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: type not inferred with useContractReads #2342

Closed
1 task done
Max-3-7 opened this issue May 14, 2023 · 3 comments
Closed
1 task done

bug: type not inferred with useContractReads #2342

Max-3-7 opened this issue May 14, 2023 · 3 comments

Comments

@Max-3-7
Copy link
Contributor

Max-3-7 commented May 14, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Package Version

1.0.2

Current Behavior

Hey guys,

I'm upgrading to wagmi v1 but I'm having issues with type inference when using useContractReads (it works fine with wagmi <v1).

Here's a simple example that is no longer working:

import { useContractReads, erc20ABI } from 'wagmi'

// an array of ERC20 addresses on mainnet
const addresses = [
    '0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32',
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
]

const contracts = addresses.map((address) => {
    return {
        abi: erc20ABI,
        address: getAddress(address)
    } as const
})

// fetch decimals, symbol and name for all tokens
const { data, isLoading } = useContractReads({
    contracts: [
        ...contracts.map((contract) => ({
        ...contract,
        functionName: 'decimals'
    })),
        ...contracts.map((contract) => ({
        ...contract,
        functionName: 'symbol'
    })),
        ...contracts.map((contract) => ({
        ...contract,
        functionName: 'name'
    }))
  ]
})

The result is of type unknown :

const data: ({
    error: Error;
    result?: undefined;
    status: "failure";
} | {
    error?: undefined;
    result: unknown;
    status: "success";
})[] | undefined

Most of the useContractReads no longer inferring the data type correctly, although there is a few of them that still works.

Let me know if you have any clue why this is happening.

Thank you very much

Expected Behavior

No response

Steps To Reproduce

No response

Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)

https://stackblitz.com/edit/new-wagmi-xemvdq?file=src/App.tsx

Anything else?

No response

@Max-3-7 Max-3-7 changed the title bug: type not infered with useContractReads bug: type not inferred with useContractReads May 14, 2023
@tmm
Copy link
Member

tmm commented May 15, 2023

Higher-order functions, like .map, don't carry type semantics very well. The best we can do in this case is by helping out the compiler a bit more asserting const in a few more places so result is now inferred as string | number.

TypeScript Playground

const data: ({
    error: Error;
    result?: undefined;
    status: "failure";
} | {
    error?: undefined;
    result: string | number;
    status: "success";
})[] | undefined

More info microsoft/TypeScript#29841

@tmm tmm closed this as completed May 15, 2023
@Max-3-7
Copy link
Contributor Author

Max-3-7 commented May 15, 2023

ah got it, thanks for the info!

Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest wagmi version. If you have any other comments you can create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants