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/index.ts b/packages/schema-type/src/typescript/index.ts index d6d60331e8..e0031491e1 100644 --- a/packages/schema-type/src/typescript/index.ts +++ b/packages/schema-type/src/typescript/index.ts @@ -2,3 +2,4 @@ export * from "./dynamicAbiTypes"; export * from "./schemaAbiTypes"; export * from "./schemaAbiTypeToDefaultValue"; export * from "./staticAbiTypes"; +export * from "./schemaAbiTypeToPrimitiveType"; diff --git a/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts b/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts new file mode 100644 index 0000000000..dba4182b98 --- /dev/null +++ b/packages/schema-type/src/typescript/schemaAbiTypeToPrimitiveType.ts @@ -0,0 +1,7 @@ +import { schemaAbiTypeToDefaultValue } from "./schemaAbiTypeToDefaultValue"; +import { SchemaAbiType } from "./schemaAbiTypes"; +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;