diff --git a/.changeset/rich-falcons-study.md b/.changeset/rich-falcons-study.md new file mode 100644 index 00000000..13cced47 --- /dev/null +++ b/.changeset/rich-falcons-study.md @@ -0,0 +1,5 @@ +--- +'abitype': patch +--- + +Added `NumberType` configuration option. `NumberType` sets the TypeScript type to use for `int` and `uint` values. diff --git a/README.md b/README.md index 34990998..c9cdd80f 100644 --- a/README.md +++ b/README.md @@ -369,11 +369,12 @@ import { TypedData } from '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. fixed array length). To do this, the following configuration options are available: -| Option | Type | Default | Description | -| ---------------------------- | ----------------- | ------- | -------------------------------------------------------------------------------------------------------- | -| `ArrayMaxDepth` | `number \| false` | `2` | Maximum depth for nested array types (e.g. `string[][]`). When `false`, there is no maximum array depth. | -| `FixedArrayLengthLowerBound` | `number` | `1` | Lower bound for fixed array length | -| `FixedArrayLengthUpperBound` | `number` | `5` | Upper bound for fixed array length | +| Option | Type | Default | Description | +| ---------------------------- | ----------------- | ------------------ | -------------------------------------------------------------------------------------------------------- | +| `ArrayMaxDepth` | `number \| false` | `2` | Maximum depth for nested array types (e.g. `string[][]`). When `false`, there is no maximum array depth. | +| `FixedArrayLengthLowerBound` | `number` | `1` | Lower bound for fixed array length | +| `FixedArrayLengthUpperBound` | `number` | `5` | Upper bound for fixed array length | +| `NumberType` | TypeScript type | `number \| bigint` | TypeScript type to use for `int` and `uint` values. | Configuration options are customizable using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html). Just extend the `Config` interface either directly in your code or in a `d.ts` file (e.g. `abi.d.ts`): diff --git a/src/config.ts b/src/config.ts index 0ece0da8..d74d977f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,6 +22,8 @@ export interface DefaultConfig { FixedArrayLengthLowerBound: 1 /** Upper bound for fixed array length */ FixedArrayLengthUpperBound: 5 + /** TypeScript type to use for `int` and `uint` values */ + NumberType: number | bigint } /** @@ -42,4 +44,7 @@ export interface ResolvedConfig { FixedArrayLengthUpperBound: Config['FixedArrayLengthUpperBound'] extends number ? Config['FixedArrayLengthUpperBound'] : DefaultConfig['FixedArrayLengthUpperBound'] + NumberType: Config['NumberType'] extends number | bigint + ? Config['NumberType'] + : DefaultConfig['NumberType'] } diff --git a/src/examples.ts b/src/examples.ts index 295bf277..cda010e8 100644 --- a/src/examples.ts +++ b/src/examples.ts @@ -217,12 +217,12 @@ export function readContracts( export function signTypedData< TTypedData extends TypedData, - Schema extends TypedDataToPrimitiveTypes, + TSchema extends TypedDataToPrimitiveTypes, >(_config: { /** Named list of all type definitions */ types: TTypedData /** Data to sign */ - value: Schema[keyof Schema] + value: TSchema[keyof TSchema] }) { return {} as Address } diff --git a/src/utils.ts b/src/utils.ts index 27af40e2..35310696 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -20,6 +20,7 @@ import { TypedDataParameter, TypedDataType, } from './abi' +import { ResolvedConfig } from './config' import { Merge, Tuple } from './types' /** @@ -44,7 +45,7 @@ type PrimitiveTypeLookup = { } & { [_ in SolidityFunction]: `${Address}${string}` } & { - [_ in SolidityInt]: number | bigint + [_ in SolidityInt]: ResolvedConfig['NumberType'] } & { [_ in SolidityString]: string } & { diff --git a/test/index.ts b/test/index.ts index 2daf29a8..ca9487e9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,13 +1,19 @@ /** * Assert parameter is of a specific type. * - * @param _value - Value that should be identical to type `T`. + * @param value - Value that should be identical to type `T`. */ -export function expectType(_value: T): void { +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export function expectType(value: T): void { // eslint-disable-next-line @typescript-eslint/no-empty-function } -export function test(_name: string, _callback: () => void) { +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export function test(name: string, _callback: () => void) { // eslint-disable-next-line @typescript-eslint/no-empty-function }