Skip to content

Commit 0bb8b56

Browse files
committed
feat: improve ts exactOptionalPropertyTypes support
1 parent 65f5e57 commit 0bb8b56

File tree

18 files changed

+114
-85
lines changed

18 files changed

+114
-85
lines changed

.changeset/orange-buckets-smoke.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@wagmi/connectors": patch
3+
"@wagmi/core": patch
4+
"@wagmi/cli": patch
5+
---
6+
7+
Improved TypeScript `'exactOptionalPropertyTypes'` support.

packages/cli/src/commands/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export async function generate(options: Generate = {}) {
125125
const content = []
126126
type Output = {
127127
plugin: Pick<Plugin, 'name'>
128-
} & Awaited<ReturnType<Required<Plugin>['run']>>
128+
} & Awaited<ReturnType<NonNullable<Plugin['run']>>>
129129
const outputs: Output[] = []
130130
spinner.start('Running plugins')
131131
for (const plugin of plugins) {

packages/cli/src/plugins/actions.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect, test } from 'vitest'
44
import { actions } from './actions.js'
55

66
test('default', async () => {
7-
const result = await actions().run({
7+
const result = await actions().run?.({
88
contracts: [
99
{
1010
name: 'erc20',
@@ -19,11 +19,11 @@ test('default', async () => {
1919
outputs: [],
2020
})
2121

22-
expect(result.imports).toMatchInlineSnapshot(`
22+
expect(result?.imports).toMatchInlineSnapshot(`
2323
"import { createReadContract, createWriteContract, createSimulateContract, createWatchContractEvent } from '@wagmi/core/codegen'
2424
"
2525
`)
26-
expect(result.content).toMatchInlineSnapshot(`
26+
expect(result?.content).toMatchInlineSnapshot(`
2727
"/**
2828
* Wraps __{@link readContract}__ with \`abi\` set to __{@link erc20Abi}__
2929
*/
@@ -117,7 +117,7 @@ test('default', async () => {
117117
})
118118

119119
test('address', async () => {
120-
const result = await actions().run({
120+
const result = await actions().run?.({
121121
contracts: [
122122
{
123123
name: 'erc20',
@@ -133,7 +133,7 @@ test('address', async () => {
133133
outputs: [],
134134
})
135135

136-
expect(result.content).toMatchInlineSnapshot(`
136+
expect(result?.content).toMatchInlineSnapshot(`
137137
"/**
138138
* Wraps __{@link readContract}__ with \`abi\` set to __{@link erc20Abi}__
139139
*/
@@ -227,7 +227,7 @@ test('address', async () => {
227227
})
228228

229229
test('legacy hook names', async () => {
230-
const result = await actions({ getActionName: 'legacy' }).run({
230+
const result = await actions({ getActionName: 'legacy' }).run?.({
231231
contracts: [
232232
{
233233
name: 'erc20',
@@ -243,7 +243,7 @@ test('legacy hook names', async () => {
243243
outputs: [],
244244
})
245245

246-
expect(result.content).toMatchInlineSnapshot(`
246+
expect(result?.content).toMatchInlineSnapshot(`
247247
"/**
248248
* Wraps __{@link readContract}__ with \`abi\` set to __{@link erc20Abi}__
249249
*/
@@ -337,7 +337,7 @@ test('legacy hook names', async () => {
337337
})
338338

339339
test('override package name', async () => {
340-
const result = await actions({ overridePackageName: 'wagmi' }).run({
340+
const result = await actions({ overridePackageName: 'wagmi' }).run?.({
341341
contracts: [
342342
{
343343
name: 'erc20',
@@ -352,7 +352,7 @@ test('override package name', async () => {
352352
outputs: [],
353353
})
354354

355-
expect(result.imports).toMatchInlineSnapshot(`
355+
expect(result?.imports).toMatchInlineSnapshot(`
356356
"import { createReadContract, createWriteContract, createSimulateContract, createWatchContractEvent } from 'wagmi/codegen'
357357
"
358358
`)

packages/cli/src/plugins/blockExplorer.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ beforeAll(() => server.listen())
1616
afterEach(() => server.resetHandlers())
1717
afterAll(() => server.close())
1818

19-
test('fetches ABI', async () => {
20-
await expect(
19+
test('fetches ABI', () => {
20+
expect(
2121
blockExplorer({
2222
apiKey,
2323
baseUrl,
@@ -26,27 +26,27 @@ test('fetches ABI', async () => {
2626
).resolves.toMatchSnapshot()
2727
})
2828

29-
test('fetches ABI with multichain deployment', async () => {
30-
await expect(
29+
test('fetches ABI with multichain deployment', () => {
30+
expect(
3131
blockExplorer({
3232
apiKey,
3333
baseUrl,
3434
contracts: [
3535
{ name: 'WagmiMintExample', address: { 1: address, 10: address } },
3636
],
37-
}).contracts(),
37+
}).contracts?.(),
3838
).resolves.toMatchSnapshot()
3939
})
4040

41-
test('fails to fetch for unverified contract', async () => {
42-
await expect(
41+
test('fails to fetch for unverified contract', () => {
42+
expect(
4343
blockExplorer({
4444
apiKey,
4545
baseUrl,
4646
contracts: [
4747
{ name: 'WagmiMintExample', address: unverifiedContractAddress },
4848
],
49-
}).contracts(),
49+
}).contracts?.(),
5050
).rejects.toThrowErrorMatchingInlineSnapshot(
5151
'[Error: Contract source code not verified]',
5252
)

packages/cli/src/plugins/etherscan.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,61 @@ beforeAll(() => server.listen())
1717
afterEach(() => server.resetHandlers())
1818
afterAll(() => server.close())
1919

20-
test('fetches ABI', async () => {
21-
await expect(
20+
test('fetches ABI', () => {
21+
expect(
2222
etherscan({
2323
apiKey,
2424
chainId: 1,
2525
contracts: [{ name: 'WagmiMintExample', address }],
26-
}).contracts(),
26+
}).contracts?.(),
2727
).resolves.toMatchSnapshot()
2828
})
2929

30-
test('fetches ABI with multichain deployment', async () => {
31-
await expect(
30+
test('fetches ABI with multichain deployment', () => {
31+
expect(
3232
etherscan({
3333
apiKey,
3434
chainId: 1,
3535
contracts: [
3636
{ name: 'WagmiMintExample', address: { 1: address, 10: address } },
3737
],
38-
}).contracts(),
38+
}).contracts?.(),
3939
).resolves.toMatchSnapshot()
4040
})
4141

42-
test('fails to fetch for unverified contract', async () => {
43-
await expect(
42+
test('fails to fetch for unverified contract', () => {
43+
expect(
4444
etherscan({
4545
apiKey,
4646
chainId: 1,
4747
contracts: [
4848
{ name: 'WagmiMintExample', address: unverifiedContractAddress },
4949
],
50-
}).contracts(),
50+
}).contracts?.(),
5151
).rejects.toThrowErrorMatchingInlineSnapshot(
5252
'[Error: Contract source code not verified]',
5353
)
5454
})
5555

56-
test('missing address for chainId', async () => {
57-
await expect(
56+
test('missing address for chainId', () => {
57+
expect(
5858
etherscan({
5959
apiKey,
6060
chainId: 1,
6161
// @ts-expect-error `chainId` and `keyof typeof contracts[number].address` mismatch
6262
contracts: [{ name: 'WagmiMintExample', address: { 10: address } }],
63-
}).contracts(),
63+
}).contracts?.(),
6464
).rejects.toThrowErrorMatchingInlineSnapshot(
6565
`[Error: No address found for chainId "1". Make sure chainId "1" is set as an address.]`,
6666
)
6767
})
6868

69-
test('invalid api key', async () => {
70-
await expect(
69+
test('invalid api key', () => {
70+
expect(
7171
etherscan({
7272
apiKey: invalidApiKey,
7373
chainId: 1,
7474
contracts: [{ name: 'WagmiMintExample', address: timeoutAddress }],
75-
}).contracts(),
75+
}).contracts?.(),
7676
).rejects.toThrowErrorMatchingInlineSnapshot('[Error: Invalid API Key]')
7777
})

packages/cli/src/plugins/fetch.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,38 @@ const parse: Fetch['parse'] = async ({ response }) => {
3333
return JSON.parse(data.result)
3434
}
3535

36-
test('fetches ABI', async () => {
37-
await expect(
36+
test('fetches ABI', () => {
37+
expect(
3838
fetch({
3939
contracts: [{ name: 'WagmiMintExample', address }],
4040
request,
4141
parse,
42-
}).contracts(),
42+
}).contracts?.(),
4343
).resolves.toMatchSnapshot()
4444
})
4545

46-
test('fails to fetch for unverified contract', async () => {
47-
await expect(
46+
test('fails to fetch for unverified contract', () => {
47+
expect(
4848
fetch({
4949
contracts: [
5050
{ name: 'WagmiMintExample', address: unverifiedContractAddress },
5151
],
5252
request,
5353
parse,
54-
}).contracts(),
54+
}).contracts?.(),
5555
).rejects.toThrowErrorMatchingInlineSnapshot(
5656
'[Error: Contract source code not verified]',
5757
)
5858
})
5959

60-
test('aborts request', async () => {
61-
await expect(
60+
test('aborts request', () => {
61+
expect(
6262
fetch({
6363
contracts: [{ name: 'WagmiMintExample', address: timeoutAddress }],
6464
request,
6565
parse,
6666
timeoutDuration: 1_000,
67-
}).contracts(),
67+
}).contracts?.(),
6868
).rejects.toThrowErrorMatchingInlineSnapshot(
6969
'[AbortError: This operation was aborted]',
7070
)
@@ -98,7 +98,7 @@ test('reads from cache', async () => {
9898
contracts: [contract],
9999
request,
100100
parse,
101-
}).contracts(),
101+
}).contracts?.(),
102102
).resolves.toMatchInlineSnapshot(`
103103
[
104104
{
@@ -149,7 +149,7 @@ test('fails and reads from cache', async () => {
149149
request,
150150
parse,
151151
timeoutDuration: 1,
152-
}).contracts(),
152+
}).contracts?.(),
153153
).resolves.toMatchInlineSnapshot(`
154154
[
155155
{

packages/cli/src/plugins/foundry.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('forge not installed', async () => {
1818
forge: {
1919
path: '/path/to/forge',
2020
},
21-
}).validate(),
21+
}).validate?.(),
2222
).rejects.toThrowErrorMatchingInlineSnapshot(`
2323
[Error: forge must be installed to use Foundry plugin.
2424
To install, follow the instructions at https://book.getfoundry.sh/getting-started/installation]
@@ -31,7 +31,7 @@ test('project does not exist', async () => {
3131
spy.mockImplementation(() => dir)
3232

3333
try {
34-
await foundry({ project: '../path/to/project' }).validate()
34+
await foundry({ project: '../path/to/project' }).validate?.()
3535
} catch (error) {
3636
expect(
3737
(error as Error).message.replace(dirname(dir), '..'),
@@ -44,15 +44,15 @@ test('validates without project', async () => {
4444
const spy = vi.spyOn(process, 'cwd')
4545
spy.mockImplementation(() => dir)
4646

47-
expect(foundry().validate()).resolves.toBeUndefined()
47+
expect(foundry().validate?.()).resolves.toBeUndefined()
4848
})
4949

5050
test('contracts', () => {
5151
expect(
5252
foundry({
5353
project: resolve(__dirname, '__fixtures__/foundry/'),
5454
exclude: ['Foo.sol/**'],
55-
}).contracts(),
55+
}).contracts?.(),
5656
).resolves.toMatchInlineSnapshot(`
5757
[
5858
{
@@ -106,7 +106,7 @@ test('contracts without project', async () => {
106106
expect(
107107
foundry({
108108
exclude: ['Foo.sol/**'],
109-
}).contracts(),
109+
}).contracts?.(),
110110
).resolves.toMatchInlineSnapshot(`
111111
[
112112
{

packages/cli/src/plugins/hardhat.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ afterEach(() => {
1313
test('validate', async () => {
1414
const temp = f.temp()
1515
expect(
16-
hardhat({ project: temp }).validate(),
16+
hardhat({ project: temp }).validate?.(),
1717
).rejects.toThrowErrorMatchingInlineSnapshot(
1818
'[Error: hardhat must be installed to use Hardhat plugin.]',
1919
)
@@ -25,7 +25,7 @@ test('project does not exist', async () => {
2525
spy.mockImplementation(() => dir)
2626

2727
try {
28-
await hardhat({ project: '../path/to/project' }).validate()
28+
await hardhat({ project: '../path/to/project' }).validate?.()
2929
} catch (error) {
3030
expect(
3131
(error as Error).message.replace(dirname(dir), '..'),
@@ -38,7 +38,7 @@ test('contracts', async () => {
3838
hardhat({
3939
project: resolve(__dirname, '__fixtures__/hardhat/'),
4040
exclude: ['Foo.sol/**'],
41-
}).contracts(),
41+
}).contracts?.(),
4242
).resolves.toMatchInlineSnapshot(`
4343
[
4444
{

0 commit comments

Comments
 (0)