Skip to content

Instantly share code, notes, and snippets.

@ashwin1014
Created February 28, 2024 12:55
Show Gist options
  • Save ashwin1014/59febf488eb605cc9d7f7800c487138f to your computer and use it in GitHub Desktop.
Save ashwin1014/59febf488eb605cc9d7f7800c487138f to your computer and use it in GitHub Desktop.
Organizing navigation types for react-native
type CreateNavigationParams<
ScreenEnum extends string,
ExtensionType extends Partial<Record<ScreenEnum, unknown>>,
> = {
[K in ScreenEnum | keyof ExtensionType]: K extends keyof ExtensionType
? ExtensionType[K]
: undefined;
};
// Usage:
enum PORTFOLIO_TABS_STACK {
BALANCES = 'BalancesTab',
LOGS = 'LogsTab',
ORDERS_CONDITIONAL = 'ConditionalOrdersTab',
ORDERS_LIMIT = 'LimitOrdersTab',
POSITIONS = 'PositionsTab',
}
type TabNavigationParamsList = CreateNavigationParams<
MAIN_TABS_STACK,
{
[MAIN_TABS_STACK.TRADES]:
| {
contractCurrency?: string;
contractType?: `${ContractType}`;
productId?: number;
symbol?: string;
}
| undefined;
[MAIN_TABS_STACK.OPTIONS_TRADES]: {
productId?: number;
symbol?: string;
};
}
>;
type PortfolioTabNavigationParamsList = CreateNavigationParams<PORTFOLIO_TABS_STACK, object>;
// using this way, we can avoid declaring undefined like below:
/**
type PortfolioTabNavigationParamsList = {
[PORTFOLIO_TABS_STACK.BALANCES]: undefined;
[PORTFOLIO_TABS_STACK.POSITIONS]: undefined;
[PORTFOLIO_TABS_STACK.ORDERS_LIMIT]: undefined;
[PORTFOLIO_TABS_STACK.ORDERS_CONDITIONAL]: undefined;
[PORTFOLIO_TABS_STACK.LOGS]: undefined;
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment