From 72e71a08c18cf826ecae993ac532e8b441ad834a Mon Sep 17 00:00:00 2001 From: nonergodic Date: Sat, 1 Feb 2025 02:03:32 -0800 Subject: [PATCH] hack to suppress tsc from expanding Chain type in tooltips etc. --- core/base/src/constants/chains.ts | 8 +++++++- core/definitions/src/layout-items/chain.ts | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/core/base/src/constants/chains.ts b/core/base/src/constants/chains.ts index 6cb8ce014..928a9baa2 100644 --- a/core/base/src/constants/chains.ts +++ b/core/base/src/constants/chains.ts @@ -70,8 +70,14 @@ const chainIdAndChainEntries = [ [10008, "MonadDevnet" ], ] as const satisfies MapLevel; +type SuppressExpansionMapping = { + readonly [key in (typeof chains)[number]]: never; +}; + +export interface UnexpandedChainUnion extends SuppressExpansionMapping {} + export const [chainIds, chains] = zip(chainIdAndChainEntries); -export type Chain = (typeof chains)[number]; +export type Chain = keyof UnexpandedChainUnion; export type ChainId = (typeof chainIds)[number]; export const chainToChainId = constMap(chainIdAndChainEntries, [1, 0]); diff --git a/core/definitions/src/layout-items/chain.ts b/core/definitions/src/layout-items/chain.ts index 14044951a..eb3e8d190 100644 --- a/core/definitions/src/layout-items/chain.ts +++ b/core/definitions/src/layout-items/chain.ts @@ -8,6 +8,9 @@ import { chainToChainId, chains, toChain } from "@wormhole-foundation/sdk-base"; const chainItemBase = { binary: "uint", size: 2 } as const; +type DontExpandAllChains = + C extends typeof chains ? Chain : C extends Chain[] ? C[number] : never; + type AllowNull = B extends true ? T | null : T; export const chainItem = < @@ -20,12 +23,12 @@ export const chainItem = < ({ ...chainItemBase, custom: { - to: (val: number): AllowNull => { + to: (val: number): AllowNull, N> => { if (val === 0) { if (!opts?.allowNull) throw new Error("ChainId 0 is not valid for this protocol and action"); - return null as AllowNull; + return null as AllowNull, N>; } const chain = toChain(val); @@ -33,7 +36,7 @@ export const chainItem = < if (!allowedChains.includes(chain)) throw new Error(`Chain ${chain} not in allowed chains ${allowedChains}`); - return chain; + return chain as DontExpandAllChains; }, from: (val: AllowNull): number => (val == null ? 0 : chainToChainId(val)), } satisfies CustomConversion>,