Skip to content

Commit

Permalink
feat: support exactOptionalPropertyTypes (#1969)
Browse files Browse the repository at this point in the history
* feat: support `exactOptionalPropertyTypes`

* Create hip-bikes-switch.md

* tweak

* chore: add gh regression test
  • Loading branch information
jxom committed Mar 17, 2024
1 parent 7723c4f commit b6f5916
Show file tree
Hide file tree
Showing 166 changed files with 903 additions and 764 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-bikes-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Support `exactOptionalPropertyTypes`.
2 changes: 1 addition & 1 deletion src/accounts/generateMnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type GenerateMnemonicErrorType = ErrorType
*/
export function generateMnemonic(
wordlist: string[],
strength?: number,
strength?: number | undefined,
): string {
return generateMnemonic_(wordlist, strength)
}
22 changes: 12 additions & 10 deletions src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export type CustomSource = {
transaction extends Parameters<serializer>[0] = Parameters<serializer>[0],
>(
transaction: transaction,
args?: {
serializer?: serializer
},
args?:
| {
serializer?: serializer | undefined
}
| undefined,
) => Promise<
IsNarrowable<
TransactionSerialized<GetTransactionType<transaction>>,
Expand Down Expand Up @@ -66,17 +68,17 @@ export type HDAccount = LocalAccount<'hd'> & {
export type HDOptions =
| {
/** The account index to use in the path (`"m/44'/60'/${accountIndex}'/0/0"`). */
accountIndex?: number
accountIndex?: number | undefined
/** The address index to use in the path (`"m/44'/60'/0'/0/${addressIndex}"`). */
addressIndex?: number
addressIndex?: number | undefined
/** The change index to use in the path (`"m/44'/60'/0'/${changeIndex}/0"`). */
changeIndex?: number
path?: never
changeIndex?: number | undefined
path?: never | undefined
}
| {
accountIndex?: never
addressIndex?: never
changeIndex?: never
accountIndex?: never | undefined
addressIndex?: never | undefined
changeIndex?: never | undefined
/** The HD path. */
path: `m/44'/60'/${string}`
}
Expand Down
2 changes: 1 addition & 1 deletion src/accounts/utils/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type SignTransactionParameters<
> = {
privateKey: Hex
transaction: transaction
serializer?: serializer
serializer?: serializer | undefined
}

export type SignTransactionReturnType<
Expand Down
8 changes: 4 additions & 4 deletions src/actions/ens/getEnsAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ import {
export type GetEnsAddressParameters = Prettify<
Pick<ReadContractParameters, 'blockNumber' | 'blockTag'> & {
/** ENSIP-9 compliant coinType used to resolve addresses for other chains */
coinType?: number
coinType?: number | undefined
/** Universal Resolver gateway URLs to use for resolving CCIP-read requests. */
gatewayUrls?: string[]
gatewayUrls?: string[] | undefined
/** Name to get the address for. */
name: string
/** Whether or not to throw errors propagated from the ENS Universal Resolver Contract. */
strict?: boolean
strict?: boolean | undefined
/** Address of ENS Universal Resolver Contract. */
universalResolverAddress?: Address
universalResolverAddress?: Address | undefined
}
>

Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsAvatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
export type GetEnsAvatarParameters = Prettify<
Omit<GetEnsTextParameters, 'key'> & {
/** Gateway urls to resolve IPFS and/or Arweave assets. */
assetGatewayUrls?: AssetGatewayUrls
assetGatewayUrls?: AssetGatewayUrls | undefined
}
>

Expand Down
6 changes: 3 additions & 3 deletions src/actions/ens/getEnsName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export type GetEnsNameParameters = Prettify<
/** Address to get ENS name for. */
address: Address
/** Universal Resolver gateway URLs to use for resolving CCIP-read requests. */
gatewayUrls?: string[]
gatewayUrls?: string[] | undefined
/** Whether or not to throw errors propagated from the ENS Universal Resolver Contract. */
strict?: boolean
strict?: boolean | undefined
/** Address of ENS Universal Resolver Contract. */
universalResolverAddress?: Address
universalResolverAddress?: Address | undefined
}
>

Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type GetEnsResolverParameters = Prettify<
/** Name to get the address for. */
name: string
/** Address of ENS Universal Resolver Contract. */
universalResolverAddress?: Address
universalResolverAddress?: Address | undefined
}
>

Expand Down
6 changes: 3 additions & 3 deletions src/actions/ens/getEnsText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export type GetEnsTextParameters = Prettify<
/** ENS name to get Text for. */
name: string
/** Universal Resolver gateway URLs to use for resolving CCIP-read requests. */
gatewayUrls?: string[]
gatewayUrls?: string[] | undefined
/** Text record to retrieve. */
key: string
/** Whether or not to throw errors propagated from the ENS Universal Resolver Contract. */
strict?: boolean
strict?: boolean | undefined
/** Address of ENS Universal Resolver Contract. */
universalResolverAddress?: Address
universalResolverAddress?: Address | undefined
}
>

Expand Down
4 changes: 2 additions & 2 deletions src/actions/getContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ test('with and without wallet client `account`', () => {

expectTypeOf(contractWithAccount.write.approve)
.parameter(1)
.extract<{ account?: Account | Address }>()
.extract<{ account?: Account | Address | undefined }>()
// @ts-expect-error
.toBeNever()
expectTypeOf(contractWithoutAccount.write.approve)
Expand All @@ -349,7 +349,7 @@ test('with and without wallet client `chain`', () => {

expectTypeOf(contractWithChain.write.approve)
.parameter(1)
.extract<{ chain?: Chain | null }>()
.extract<{ chain?: Chain | null | undefined }>()
// @ts-expect-error
.toBeNever()
expectTypeOf(contractWithoutChain.write.approve)
Expand Down
6 changes: 3 additions & 3 deletions src/actions/getContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ type KeyedClient<
TAccount extends Account | undefined = Account | undefined,
> =
| {
public?: Client<TTransport, TChain>
public?: Client<TTransport, TChain> | undefined
wallet: Client<TTransport, TChain, TAccount>
}
| {
public: Client<TTransport, TChain>
wallet?: Client<TTransport, TChain, TAccount>
wallet?: Client<TTransport, TChain, TAccount> | undefined
}

export type GetContractParameters<
Expand Down Expand Up @@ -786,7 +786,7 @@ export function getContract<
* @internal exporting for testing only
*/
export function getFunctionParameters(
values: [args?: readonly unknown[], options?: object],
values: [args?: readonly unknown[] | undefined, options?: object | undefined],
) {
const hasArgs = values.length && Array.isArray(values[0])
const args = hasArgs ? values[0]! : []
Expand Down
28 changes: 16 additions & 12 deletions src/actions/public/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import type {
} from '../../types/rpc.js'
import type { StateMapping, StateOverride } from '../../types/stateOverride.js'
import type { TransactionRequest } from '../../types/transaction.js'
import type { UnionOmit } from '../../types/utils.js'
import type { ExactPartial, UnionOmit } from '../../types/utils.js'
import {
type DecodeFunctionResultErrorType,
decodeFunctionResult,
Expand Down Expand Up @@ -90,24 +90,24 @@ export type FormattedCall<
export type CallParameters<
TChain extends Chain | undefined = Chain | undefined,
> = UnionOmit<FormattedCall<TChain>, 'from'> & {
account?: Account | Address
batch?: boolean
account?: Account | Address | undefined
batch?: boolean | undefined
} & (
| {
/** The balance of the account at a block number. */
blockNumber?: bigint
blockTag?: never
blockNumber?: bigint | undefined
blockTag?: never | undefined
}
| {
blockNumber?: never
blockNumber?: never | undefined
/**
* The balance of the account at a block tag.
* @default 'latest'
*/
blockTag?: BlockTag
blockTag?: BlockTag | undefined
}
) & {
stateOverride?: StateOverride
stateOverride?: StateOverride | undefined
}

export type CallReturnType = { data: Hex | undefined }
Expand Down Expand Up @@ -219,8 +219,12 @@ export async function call<TChain extends Chain | undefined>(
const response = await client.request({
method: 'eth_call',
params: rpcStateOverride
? [request as Partial<RpcTransactionRequest>, block, rpcStateOverride]
: [request as Partial<RpcTransactionRequest>, block],
? [
request as ExactPartial<RpcTransactionRequest>,
block,
rpcStateOverride,
]
: [request as ExactPartial<RpcTransactionRequest>, block],
})
if (response === '0x') return { data: undefined }
return { data: response }
Expand Down Expand Up @@ -262,7 +266,7 @@ type ScheduleMulticallParameters<TChain extends Chain | undefined> = Pick<
'blockNumber' | 'blockTag'
> & {
data: Hex
multicallAddress?: Address
multicallAddress?: Address | undefined
to: Address
}

Expand Down Expand Up @@ -422,7 +426,7 @@ export type ParseStateOverrideErrorType =
| ParseAccountStateOverrideErrorType

export function parseStateOverride(
args?: StateOverride,
args?: StateOverride | undefined,
): RpcStateOverride | undefined {
if (!args) return undefined
const rpcStateOverride: RpcStateOverride = {}
Expand Down
15 changes: 9 additions & 6 deletions src/actions/public/createContractEventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,29 @@ export type CreateContractEventFilterParameters<
fromBlock extends BlockNumber | BlockTag | undefined = undefined,
toBlock extends BlockNumber | BlockTag | undefined = undefined,
> = {
address?: Address | Address[]
address?: Address | Address[] | undefined
abi: abi
eventName?: eventName | ContractEventName<abi> | undefined
fromBlock?: fromBlock | BlockNumber | BlockTag
fromBlock?: fromBlock | BlockNumber | BlockTag | undefined
/**
* Whether or not the logs must match the indexed/non-indexed arguments in the event ABI item.
* @default false
*/
strict?: strict | boolean | undefined
toBlock?: toBlock | BlockNumber | BlockTag
toBlock?: toBlock | BlockNumber | BlockTag | undefined
} & (undefined extends eventName
? {
args?: never
args?: never | undefined
}
: MaybeExtractEventArgsFromAbi<abi, eventName> extends infer TEventFilterArgs
? {
args?: TEventFilterArgs | (args extends TEventFilterArgs ? args : never)
args?:
| TEventFilterArgs
| (args extends TEventFilterArgs ? args : never)
| undefined
}
: {
args?: never
args?: never | undefined
})

export type CreateContractEventFilterReturnType<
Expand Down
42 changes: 21 additions & 21 deletions src/actions/public/createEventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export type CreateEventFilterParameters<
| MaybeExtractEventArgsFromAbi<TAbiEvents, _EventName>
| undefined = undefined,
> = {
address?: Address | Address[]
fromBlock?: TFromBlock | BlockNumber | BlockTag
toBlock?: TToBlock | BlockNumber | BlockTag
address?: Address | Address[] | undefined
fromBlock?: TFromBlock | BlockNumber | BlockTag | undefined
toBlock?: TToBlock | BlockNumber | BlockTag | undefined
} & (MaybeExtractEventArgsFromAbi<
TAbiEvents,
_EventName
Expand All @@ -51,44 +51,44 @@ export type CreateEventFilterParameters<
| TEventFilterArgs
| (_Args extends TEventFilterArgs ? _Args : never)
event: TAbiEvent
events?: never
events?: never | undefined
/**
* Whether or not the logs must match the indexed/non-indexed arguments on `event`.
* @default false
*/
strict?: TStrict
strict?: TStrict | undefined
}
| {
args?: never
event?: TAbiEvent
events?: never
args?: never | undefined
event?: TAbiEvent | undefined
events?: never | undefined
/**
* Whether or not the logs must match the indexed/non-indexed arguments on `event`.
* @default false
*/
strict?: TStrict
strict?: TStrict | undefined
}
| {
args?: never
event?: never
events: TAbiEvents
args?: never | undefined
event?: never | undefined
events: TAbiEvents | undefined
/**
* Whether or not the logs must match the indexed/non-indexed arguments on `event`.
* @default false
*/
strict?: TStrict
strict?: TStrict | undefined
}
| {
args?: never
event?: never
events?: never
strict?: never
args?: never | undefined
event?: never | undefined
events?: never | undefined
strict?: never | undefined
}
: {
args?: never
event?: never
events?: never
strict?: never
args?: never | undefined
event?: never | undefined
events?: never | undefined
strict?: never | undefined
})

export type CreateEventFilterReturnType<
Expand Down
8 changes: 4 additions & 4 deletions src/actions/public/estimateFeesPerGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type EstimateFeesPerGasParameters<
*
* @default 'eip1559'
*/
type?: type | FeeValuesType
type?: type | FeeValuesType | undefined
} & GetChainParameter<chain, chainOverride>

export type EstimateFeesPerGasReturnType<
Expand Down Expand Up @@ -86,7 +86,7 @@ export async function estimateFeesPerGas<
type extends FeeValuesType = 'eip1559',
>(
client: Client<Transport, chain>,
args?: EstimateFeesPerGasParameters<chain, chainOverride, type>,
args?: EstimateFeesPerGasParameters<chain, chainOverride, type> | undefined,
): Promise<EstimateFeesPerGasReturnType<type>> {
return internal_estimateFeesPerGas(client, args as any)
}
Expand All @@ -98,8 +98,8 @@ export async function internal_estimateFeesPerGas<
>(
client: Client<Transport, chain>,
args: EstimateFeesPerGasParameters<chain, chainOverride, type> & {
block?: Block
request?: PrepareTransactionRequestParameters
block?: Block | undefined
request?: PrepareTransactionRequestParameters | undefined
},
): Promise<EstimateFeesPerGasReturnType<type>> {
const {
Expand Down

0 comments on commit b6f5916

Please sign in to comment.