Skip to content

Commit

Permalink
refactor: register case
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Jun 19, 2024
1 parent 2d485c6 commit be386da
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 107 deletions.
34 changes: 17 additions & 17 deletions docs/pages/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ ABIType's types are customizable using [declaration merging](https://www.typescr
```ts twoslash
declare module 'abitype' {
export interface Register {
BigIntType: bigint & { foo: 'bar' }
bigIntType: bigint & { foo: 'bar' }
}
}

import { ResolvedRegister } from 'abitype'
type Result = ResolvedRegister['BigIntType']
type Result = ResolvedRegister['bigIntType']
// ^?


Expand All @@ -31,7 +31,7 @@ If you are using ABIType via another package (e.g. [`viem`](https://viem.sh)), y
```ts
declare module 'viem/node_modules/abitype' {
export interface Register {
BigIntType: MyCustomBigIntType
bigIntType: MyCustomBigIntType
}
}
```
Expand All @@ -42,10 +42,10 @@ declare module 'viem/node_modules/abitype' {
ABIType tries to strike a balance between type exhaustiveness and speed with sensible defaults. In some cases, you might want to tune your configuration (e.g. use a custom `bigint` type). To do this, the following configuration options are available:

:::warning
When configuring `ArrayMaxDepth`, `FixedArrayMinLength`, and `FixedArrayMaxLength`, there are trade-offs. For example, choosing a non-false value for `ArrayMaxDepth` and increasing the range between `FixedArrayMinLength` and `FixedArrayMaxLength` will make your types more exhaustive, but will also slow down the compiler for type checking, autocomplete, etc.
When configuring `arrayMaxDepth`, `fixedArrayMinLength`, and `fixedArrayMaxLength`, there are trade-offs. For example, choosing a non-false value for `arrayMaxDepth` and increasing the range between `fixedArrayMinLength` and `fixedArrayMaxLength` will make your types more exhaustive, but will also slow down the compiler for type checking, autocomplete, etc.
:::

### `AddressType`
### `addressType`

TypeScript type to use for `address` values.

Expand All @@ -55,12 +55,12 @@ TypeScript type to use for `address` values.
```ts twoslash
declare module 'abitype' {
export interface Register {
AddressType: `0x${string}`
addressType: `0x${string}`
}
}
```

### `ArrayMaxDepth`
### `arrayMaxDepth`

Maximum depth for nested array types (e.g. `string[][]`). When `false`, there is no maximum array depth.

Expand All @@ -75,7 +75,7 @@ declare module 'abitype' {
}
```

### `BigIntType`
### `bigIntType`

TypeScript type to use for `int<M>` and `uint<M>` values, where `M > 48`.

Expand All @@ -85,12 +85,12 @@ TypeScript type to use for `int<M>` and `uint<M>` values, where `M > 48`.
```ts twoslash
declare module 'abitype' {
export interface Register {
BigIntType: bigint
bigIntType: bigint
}
}
```

### `BytesType`
### `bytesType`

TypeScript type to use for `bytes<M>` values.

Expand All @@ -100,15 +100,15 @@ TypeScript type to use for `bytes<M>` values.
```ts twoslash
declare module 'abitype' {
export interface Register {
BytesType: {
bytesType: {
inputs: `0x${string}`
outputs: `0x${string}`
}
}
}
```

### `FixedArrayMinLength`
### `fixedArrayMinLength`

Lower bound for fixed-length arrays.

Expand All @@ -123,7 +123,7 @@ declare module 'abitype' {
}
```

### `FixedArrayMaxLength`
### `fixedArrayMaxLength`

Upper bound for fixed-length arrays.

Expand All @@ -138,7 +138,7 @@ declare module 'abitype' {
}
```

### `IntType`
### `intType`

TypeScript type to use for `int<M>` and `uint<M>` values, where `M <= 48`.

Expand All @@ -148,12 +148,12 @@ TypeScript type to use for `int<M>` and `uint<M>` values, where `M <= 48`.
```ts twoslash
declare module 'abitype' {
export interface Register {
IntType: number
intType: number
}
}
```

### `StrictAbiType`
### `strictAbiType`

When set, validates `AbiParameter`'s `type` against `AbiType`.

Expand All @@ -163,7 +163,7 @@ When set, validates `AbiParameter`'s `type` against `AbiType`.
```ts twoslash
declare module 'abitype' {
export interface Register {
StrictAbiType: false
strictAbiType: false
}
}
```
Expand Down
15 changes: 3 additions & 12 deletions packages/abitype/jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
"./zod": "./src/exports/zod.ts"
},
"publish": {
"include": [
"LICENSE",
"README.md",
"CHANGELOG.md",
"src/**/*.ts"
],
"exclude": [
"src/**/*.bench.ts",
"src/**/*.test.ts",
"src/**/*.test-d.ts"
]
"include": ["LICENSE", "README.md", "CHANGELOG.md", "src/**/*.ts"],
"exclude": ["src/**/*.bench.ts", "src/**/*.test.ts", "src/**/*.test-d.ts"]
}
}
}
14 changes: 7 additions & 7 deletions packages/abitype/src/abi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ResolvedRegister } from './register.js'
import type { Pretty, Range } from './types.js'

export type Address = ResolvedRegister['AddressType']
export type Address = ResolvedRegister['addressType']

////////////////////////////////////////////////////////////////////////////////////////////////////
// Solidity Types
Expand Down Expand Up @@ -37,8 +37,8 @@ export type SolidityInt = `${'u' | ''}int${MBits}` // `(u)int<M>`: (un)signed in
// | `${'u' | ''}fixed${MBits}x${Range<1, 20>[number]}`

export type SolidityFixedArrayRange = Range<
ResolvedRegister['FixedArrayMinLength'],
ResolvedRegister['FixedArrayMaxLength']
ResolvedRegister['fixedArrayMinLength'],
ResolvedRegister['fixedArrayMaxLength']
>[number]
export type SolidityFixedArraySizeLookup = {
[Prop in SolidityFixedArrayRange as `${Prop}`]: Prop
Expand All @@ -51,9 +51,9 @@ export type SolidityFixedArraySizeLookup = {
type _BuildArrayTypes<
T extends string,
Depth extends readonly number[] = [],
> = ResolvedRegister['ArrayMaxDepth'] extends false
> = ResolvedRegister['arrayMaxDepth'] extends false
? `${T}[${string}]`
: Depth['length'] extends ResolvedRegister['ArrayMaxDepth']
: Depth['length'] extends ResolvedRegister['arrayMaxDepth']
? T
: T extends `${any}[${SolidityFixedArrayRange | ''}]`
? _BuildArrayTypes<
Expand Down Expand Up @@ -87,7 +87,7 @@ export type AbiType =
| SolidityInt
| SolidityString
| SolidityTuple
type ResolvedAbiType = ResolvedRegister['StrictAbiType'] extends true
type ResolvedAbiType = ResolvedRegister['strictAbiType'] extends true
? AbiType
: string

Expand Down Expand Up @@ -221,7 +221,7 @@ export type Abi = readonly (
export type TypedDataDomain = {
chainId?: number | undefined
name?: string | undefined
salt?: ResolvedRegister['BytesType']['outputs'] | undefined
salt?: ResolvedRegister['bytesType']['outputs'] | undefined
verifyingContract?: Address | undefined
version?: string | undefined
}
Expand Down
2 changes: 1 addition & 1 deletion packages/abitype/src/human-readable/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export type _ValidateAbiParameter<abiParameter extends AbiParameter> =
) extends infer parameter
? // Validate `type` against `AbiType`
(
ResolvedRegister['StrictAbiType'] extends true
ResolvedRegister['strictAbiType'] extends true
? parameter extends { type: AbiType }
? parameter
: Merge<
Expand Down
16 changes: 8 additions & 8 deletions packages/abitype/src/register.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { assertType, test } from 'vitest'
import type { ResolvedRegister } from './register.js'

test('ResolvedRegister', () => {
assertType<ResolvedRegister['ArrayMaxDepth']>(false)
assertType<ResolvedRegister['FixedArrayMinLength']>(1)
assertType<ResolvedRegister['FixedArrayMaxLength']>(99)
assertType<ResolvedRegister['arrayMaxDepth']>(false)
assertType<ResolvedRegister['fixedArrayMinLength']>(1)
assertType<ResolvedRegister['fixedArrayMaxLength']>(99)

type AddressType = ResolvedRegister['AddressType']
type AddressType = ResolvedRegister['addressType']
assertType<AddressType>('0x0000000000000000000000000000000000000000')

type BytesType = ResolvedRegister['BytesType']
type BytesType = ResolvedRegister['bytesType']
assertType<BytesType>({
inputs: '0xfoobarbaz',
outputs: '0xfoobarbaz',
})

type IntType = ResolvedRegister['IntType']
type IntType = ResolvedRegister['intType']
assertType<IntType>(123)

type BigIntType = ResolvedRegister['BigIntType']
type BigIntType = ResolvedRegister['bigIntType']
assertType<BigIntType>(123n)

type StrictAbiType = ResolvedRegister['StrictAbiType']
type StrictAbiType = ResolvedRegister['strictAbiType']
assertType<StrictAbiType>(false)
})
Loading

0 comments on commit be386da

Please sign in to comment.