Skip to content

Commit 058c8c1

Browse files
committed
fix(core,react): fix read contract return type inference for overloads
1 parent 29f95df commit 058c8c1

File tree

11 files changed

+686
-666
lines changed

11 files changed

+686
-666
lines changed

.changeset/sixty-sloths-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wagmi": patch
3+
---
4+
5+
Fixed `useReadContract` return type inference for ABI function overloads.

.changeset/ten-cows-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wagmi/core": patch
3+
---
4+
5+
Fixed `readContract` return type inference for ABI function overloads.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"react-dom": "catalog:",
5959
"sherif": "^1.0.0",
6060
"simple-git-hooks": "^2.11.1",
61-
"typescript": "5.9.2",
61+
"typescript": "5.9.3",
6262
"viem": "2.38.0",
6363
"vite-plugin-react-fallback-throttle": "^0.1.1",
6464
"vitest": "^4.0.8",

packages/core/src/actions/estimateFeesPerGas.test-d.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,36 @@ import { expectTypeOf, test } from 'vitest'
33
import { estimateFeesPerGas } from './estimateFeesPerGas.js'
44

55
test('types', async () => {
6-
const default_ = await estimateFeesPerGas(config)
7-
expectTypeOf(default_).toMatchTypeOf<{
8-
gasPrice?: undefined
9-
maxFeePerGas: bigint
10-
maxPriorityFeePerGas: bigint
11-
}>()
6+
// default
7+
{
8+
const result = await estimateFeesPerGas(config, {})
9+
expectTypeOf(result).toEqualTypeOf<{
10+
gasPrice?: undefined
11+
maxFeePerBlobGas?: undefined
12+
maxFeePerGas: bigint
13+
maxPriorityFeePerGas: bigint
14+
}>()
15+
}
1216

13-
const legacy = await estimateFeesPerGas(config, { type: 'legacy' })
14-
expectTypeOf(legacy).toMatchTypeOf<{
15-
gasPrice: bigint
16-
maxFeePerGas?: undefined
17-
maxPriorityFeePerGas?: undefined
18-
}>()
17+
// legacy
18+
{
19+
const result = await estimateFeesPerGas(config, { type: 'legacy' })
20+
expectTypeOf(result).toEqualTypeOf<{
21+
gasPrice: bigint
22+
maxFeePerBlobGas?: undefined
23+
maxFeePerGas?: undefined
24+
maxPriorityFeePerGas?: undefined
25+
}>()
26+
}
1927

20-
const eip1559 = await estimateFeesPerGas(config, { type: 'eip1559' })
21-
expectTypeOf(eip1559).toMatchTypeOf<{
22-
gasPrice?: undefined
23-
maxFeePerGas: bigint
24-
maxPriorityFeePerGas: bigint
25-
}>()
28+
// eip1559
29+
{
30+
const result = await estimateFeesPerGas(config, { type: 'eip1559' })
31+
expectTypeOf(result).toEqualTypeOf<{
32+
gasPrice?: undefined
33+
maxFeePerBlobGas?: undefined
34+
maxFeePerGas: bigint
35+
maxPriorityFeePerGas: bigint
36+
}>()
37+
}
2638
})

packages/core/src/actions/getTransaction.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ test('chain formatters', async () => {
1111
transports: { [celo.id]: http(), [mainnet.id]: http() },
1212
})
1313
const result = await getTransaction(config, { hash: '0x123' })
14+
// @ts-expect-error
15+
result.feeCurrency
1416
if (result.chainId === celo.id) {
1517
expectTypeOf(result.feeCurrency).toEqualTypeOf<`0x${string}` | null>()
1618
}

packages/core/src/actions/readContract.test-d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ test('overloads', async () => {
3535
functionName: 'foo',
3636
args: ['0x'],
3737
})
38-
// @ts-ignore – TODO: Fix https://github.com/wevm/viem/issues/1916
3938
assertType<string>(result3)
4039

4140
const result4 = await readContract(config, {
@@ -47,7 +46,6 @@ test('overloads', async () => {
4746
assertType<{
4847
foo: `0x${string}`
4948
bar: `0x${string}`
50-
// @ts-ignore – TODO: Fix https://github.com/wevm/viem/issues/1916
5149
}>(result4)
5250
})
5351

packages/core/src/actions/readContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function readContract<
4545
config extends Config,
4646
const abi extends Abi | readonly unknown[],
4747
functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
48-
args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
48+
const args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
4949
>(
5050
config: config,
5151
parameters: ReadContractParameters<abi, functionName, args, config>,

packages/core/src/actions/writeContract.bench-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const abiOverload = parseAbi([
2626
test('overloads', () => {
2727
type Result = WriteContractParameters<typeof abiOverload, 'foo'>
2828
const res = {} as Result
29-
attest.instantiations([17325, 'instantiations'])
29+
attest.instantiations([21691, 'instantiations'])
3030
attest<
3131
| readonly []
3232
| readonly [account: `0x${string}`]

packages/react/src/hooks/useReadContract.test-d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ test('overloads', () => {
6666
functionName: 'foo',
6767
args: ['0x'],
6868
})
69-
// @ts-ignore – TODO: Fix https://github.com/wevm/viem/issues/1916
7069
assertType<string | undefined>(result3.data)
7170

7271
const result4 = useReadContract({
@@ -81,7 +80,6 @@ test('overloads', () => {
8180
bar: `0x${string}`
8281
}
8382
| undefined
84-
// @ts-ignore – TODO: Fix https://github.com/wevm/viem/issues/1916
8583
>(result4.data)
8684
})
8785

packages/react/src/hooks/useReadContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export type UseReadContractReturnType<
6363
export function useReadContract<
6464
const abi extends Abi | readonly unknown[],
6565
functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
66-
args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
66+
const args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
6767
config extends Config = ResolvedRegister['config'],
6868
selectData = ReadContractData<abi, functionName, args>,
6969
>(

0 commit comments

Comments
 (0)