Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hack to suppress tsc from expanding Chain type in tooltips etc. #791

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading