From ce9f69e79ec8061d887116e317945458c6989ba8 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 23 Jun 2023 11:44:10 +0100 Subject: [PATCH] add support for arrays to LiteralToBroad, fix static and dynamic abi to primitive --- .../src/typescript/dynamicAbiTypes.ts | 6 ++++-- .../typescript/schemaAbiTypeToPrimitiveType.ts | 14 +------------- .../schema-type/src/typescript/staticAbiTypes.ts | 6 ++++-- packages/schema-type/src/typescript/utils.ts | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/schema-type/src/typescript/dynamicAbiTypes.ts b/packages/schema-type/src/typescript/dynamicAbiTypes.ts index 0bcc7a7a6c..7c5c642e4c 100644 --- a/packages/schema-type/src/typescript/dynamicAbiTypes.ts +++ b/packages/schema-type/src/typescript/dynamicAbiTypes.ts @@ -1,5 +1,6 @@ import { Hex } from "viem"; import { StaticAbiType, DynamicAbiType } from "./schemaAbiTypes"; +import { LiteralToBroad } from "./utils"; // Variable-length ABI types, where their lengths are encoded by a PackedCounter within the record @@ -112,8 +113,9 @@ export const dynamicAbiTypeToDefaultValue = { string: "", } as const satisfies Record; -export type DynamicAbiTypeToPrimitiveType = - (typeof dynamicAbiTypeToDefaultValue)[TDynamicAbiType]; +export type DynamicAbiTypeToPrimitiveType = LiteralToBroad< + (typeof dynamicAbiTypeToDefaultValue)[TDynamicAbiType] +>; export type ArrayAbiTypeToStaticAbiType = T extends `${infer StaticAbiType}[]` ? StaticAbiType diff --git a/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts b/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts index 2ca681a1f0..dba4182b98 100644 --- a/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts +++ b/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts @@ -1,18 +1,6 @@ -import { Hex } from "viem"; import { schemaAbiTypeToDefaultValue } from "./schemaAbiTypeToDefaultValue"; import { SchemaAbiType } from "./schemaAbiTypes"; - -type LiteralToBroad = T extends number - ? number - : T extends bigint - ? bigint - : T extends Hex - ? Hex - : T extends boolean - ? boolean - : T extends string - ? string - : never; +import { LiteralToBroad } from "./utils"; export type SchemaAbiTypeToPrimitiveType = LiteralToBroad< (typeof schemaAbiTypeToDefaultValue)[T] diff --git a/packages/schema-type/src/typescript/staticAbiTypes.ts b/packages/schema-type/src/typescript/staticAbiTypes.ts index 6e00977f4b..23ce59e196 100644 --- a/packages/schema-type/src/typescript/staticAbiTypes.ts +++ b/packages/schema-type/src/typescript/staticAbiTypes.ts @@ -1,5 +1,6 @@ import { Hex } from "viem"; import { StaticAbiType } from "./schemaAbiTypes"; +import { LiteralToBroad } from "./utils"; // Fixed-length ABI types @@ -109,8 +110,9 @@ export const staticAbiTypeToDefaultValue = { address: "0x0000000000000000000000000000000000000000", } as const satisfies Record; -export type StaticAbiTypeToPrimitiveType = - (typeof staticAbiTypeToDefaultValue)[TStaticAbiType]; +export type StaticAbiTypeToPrimitiveType = LiteralToBroad< + (typeof staticAbiTypeToDefaultValue)[TStaticAbiType] +>; export const staticAbiTypeToByteLength = { uint8: 1, diff --git a/packages/schema-type/src/typescript/utils.ts b/packages/schema-type/src/typescript/utils.ts index 9e7a8c1f4a..9b75d371c0 100644 --- a/packages/schema-type/src/typescript/utils.ts +++ b/packages/schema-type/src/typescript/utils.ts @@ -1,5 +1,21 @@ +import { Hex } from "viem"; + export type TupleSplit = O["length"] extends N ? [O, T] : T extends readonly [infer F, ...infer R] ? TupleSplit : [O, T]; + +export type LiteralToBroad = T extends Array + ? LiteralToBroad[] + : T extends number + ? number + : T extends bigint + ? bigint + : T extends Hex + ? Hex + : T extends boolean + ? boolean + : T extends string + ? string + : never;