Skip to content

Commit

Permalink
feat(schema-type): add type narrowing isStaticAbiType (#1196)
Browse files Browse the repository at this point in the history
Co-authored-by: alvarius <alvarius@lattice.xyz>
  • Loading branch information
holic and alvrs authored Jul 27, 2023
1 parent afdba79 commit b02f9d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-horses-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/schema-type": minor
---

add type narrowing `isStaticAbiType`
6 changes: 3 additions & 3 deletions packages/schema-type/src/typescript/dynamicAbiTypes.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -124,6 +124,6 @@ export type DynamicAbiTypeToPrimitiveType<TDynamicAbiType extends DynamicAbiType
(typeof dynamicAbiTypeToDefaultValue)[TDynamicAbiType]
>;

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);
}
6 changes: 5 additions & 1 deletion packages/schema-type/src/typescript/staticAbiTypes.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -217,3 +217,7 @@ export const staticAbiTypeToByteLength = {
bool: 1,
address: 20,
} as const satisfies Record<StaticAbiType, number>;

export function isStaticAbiType(abiType: string): abiType is StaticAbiType {
return staticAbiTypes.includes(abiType as StaticAbiType);
}

0 comments on commit b02f9d0

Please sign in to comment.