From b02f9d0e43089e5f9b46d817ea2032ce0a1b0b07 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Thu, 27 Jul 2023 10:32:21 -0700 Subject: [PATCH] feat(schema-type): add type narrowing isStaticAbiType (#1196) Co-authored-by: alvarius --- .changeset/pink-horses-deny.md | 5 +++++ packages/schema-type/src/typescript/dynamicAbiTypes.ts | 6 +++--- packages/schema-type/src/typescript/staticAbiTypes.ts | 6 +++++- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .changeset/pink-horses-deny.md diff --git a/.changeset/pink-horses-deny.md b/.changeset/pink-horses-deny.md new file mode 100644 index 0000000000..509fcf69a3 --- /dev/null +++ b/.changeset/pink-horses-deny.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/schema-type": minor +--- + +add type narrowing `isStaticAbiType` diff --git a/packages/schema-type/src/typescript/dynamicAbiTypes.ts b/packages/schema-type/src/typescript/dynamicAbiTypes.ts index 5bd0654e63..8c4549c2f2 100644 --- a/packages/schema-type/src/typescript/dynamicAbiTypes.ts +++ b/packages/schema-type/src/typescript/dynamicAbiTypes.ts @@ -1,5 +1,5 @@ import { Hex } from "viem"; -import { DynamicAbiType, SchemaAbiType } from "./schemaAbiTypes"; +import { DynamicAbiType, SchemaAbiType, dynamicAbiTypes } from "./schemaAbiTypes"; import { LiteralToBroad } from "./utils"; import { isArrayAbiType } from "./arrayAbiTypes"; @@ -124,6 +124,6 @@ export type DynamicAbiTypeToPrimitiveType; -export function isDynamicAbiType(abiType: SchemaAbiType): abiType is DynamicAbiType { - return isArrayAbiType(abiType) || abiType === "bytes" || abiType === "string"; +export function isDynamicAbiType(abiType: string): abiType is DynamicAbiType { + return dynamicAbiTypes.includes(abiType as DynamicAbiType); } diff --git a/packages/schema-type/src/typescript/staticAbiTypes.ts b/packages/schema-type/src/typescript/staticAbiTypes.ts index 5c27a347f3..61938a4d93 100644 --- a/packages/schema-type/src/typescript/staticAbiTypes.ts +++ b/packages/schema-type/src/typescript/staticAbiTypes.ts @@ -1,5 +1,5 @@ import { Hex } from "viem"; -import { StaticAbiType } from "./schemaAbiTypes"; +import { StaticAbiType, staticAbiTypes } from "./schemaAbiTypes"; import { LiteralToBroad } from "./utils"; // Fixed-length ABI types @@ -217,3 +217,7 @@ export const staticAbiTypeToByteLength = { bool: 1, address: 20, } as const satisfies Record; + +export function isStaticAbiType(abiType: string): abiType is StaticAbiType { + return staticAbiTypes.includes(abiType as StaticAbiType); +}