Skip to content

Commit

Permalink
hack to suppress tsc from expanding Chain type in tooltips etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
nonergodic committed Feb 1, 2025
1 parent b90c148 commit 72e71a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 7 additions & 1 deletion core/base/src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ const chainIdAndChainEntries = [
[10008, "MonadDevnet" ],
] as const satisfies MapLevel<number, string>;

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]);
Expand Down
9 changes: 6 additions & 3 deletions core/definitions/src/layout-items/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { chainToChainId, chains, toChain } from "@wormhole-foundation/sdk-base";

const chainItemBase = { binary: "uint", size: 2 } as const;

type DontExpandAllChains<C> =
C extends typeof chains ? Chain : C extends Chain[] ? C[number] : never;

type AllowNull<T, B extends boolean> = B extends true ? T | null : T;

export const chainItem = <
Expand All @@ -20,20 +23,20 @@ export const chainItem = <
({
...chainItemBase,
custom: {
to: (val: number): AllowNull<C[number], N> => {
to: (val: number): AllowNull<DontExpandAllChains<C>, N> => {
if (val === 0) {
if (!opts?.allowNull)
throw new Error("ChainId 0 is not valid for this protocol and action");

return null as AllowNull<C[number], N>;
return null as AllowNull<DontExpandAllChains<C>, N>;
}

const chain = toChain(val);
const allowedChains = opts?.allowedChains ?? chains;
if (!allowedChains.includes(chain))
throw new Error(`Chain ${chain} not in allowed chains ${allowedChains}`);

return chain;
return chain as DontExpandAllChains<C>;
},
from: (val: AllowNull<C[number], N>): number => (val == null ? 0 : chainToChainId(val)),
} satisfies CustomConversion<number, AllowNull<C[number], N>>,
Expand Down

0 comments on commit 72e71a0

Please sign in to comment.