From eaebe5d46dfaf7a14f3b8d4a4d60e974c7dc2625 Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Mon, 23 Sep 2019 22:34:14 +0200 Subject: [PATCH] fix: add type aliases for switch navigator. fixes #6324 --- .../typescript/react-navigation.d.ts | 128 +++++++++++++++--- 1 file changed, 108 insertions(+), 20 deletions(-) diff --git a/packages/react-navigation/typescript/react-navigation.d.ts b/packages/react-navigation/typescript/react-navigation.d.ts index 8c04fd3d..12eba275 100644 --- a/packages/react-navigation/typescript/react-navigation.d.ts +++ b/packages/react-navigation/typescript/react-navigation.d.ts @@ -164,9 +164,13 @@ declare module 'react-navigation' { params?: NavigationParams; }; - getComponentForRouteName: (routeName: string) => NavigationComponent<{}, NavigationScreenProp>; + getComponentForRouteName: ( + routeName: string + ) => NavigationComponent<{}, NavigationScreenProp>; - getComponentForState: (state: State) => NavigationComponent<{}, NavigationScreenProp>; + getComponentForState: ( + state: State + ) => NavigationComponent<{}, NavigationScreenProp>; getActionCreators: ( route: NavigationRoute, @@ -186,20 +190,36 @@ declare module 'react-navigation' { getScreenOptions: NavigationScreenOptionsGetter; } - export type NavigationScreenComponent = React.ComponentType & { - navigationOptions?: NavigationScreenConfig; + export type NavigationScreenComponent< + Options, + NavigationScreenPropType + > = React.ComponentType & { + navigationOptions?: NavigationScreenConfig< + Options, + NavigationScreenPropType + >; }; - export interface NavigationScreenConfigProps { + export interface NavigationScreenConfigProps< + NavigationScreenPropType, + ScreenProps = unknown + > { navigation: NavigationScreenPropType; screenProps: ScreenProps; theme: SupportedThemes; } - export type NavigationScreenConfig = + export type NavigationScreenConfig< + Options, + NavigationScreenPropType, + ScreenProps = unknown + > = | Options | (( - navigationOptionsContainer: NavigationScreenConfigProps & { + navigationOptionsContainer: NavigationScreenConfigProps< + NavigationScreenPropType, + ScreenProps + > & { navigationOptions: Options; } ) => Options); @@ -208,7 +228,10 @@ declare module 'react-navigation' { | NavigationScreenComponent | NavigationNavigator; - export type NavigationNavigator = React.ComponentType> & { + export type NavigationNavigator< + Options, + NavigationPropType + > = React.ComponentType> & { router: NavigationRouter; navigationOptions?: NavigationScreenConfig; }; @@ -399,6 +422,33 @@ declare module 'react-navigation' { config?: NavigationSwitchRouterConfig ): NavigationRouter; + export type NavigationSwitchProp< + State = NavigationRoute, + Params = NavigationParams + > = NavigationScreenProp & { + jumpTo(routeName: string, key?: string): void; + }; + + export type NavigationSwitchScreenProps< + Params = NavigationParams, + ScreenProps = unknown + > = { + theme: SupportedThemes; + navigation: NavigationSwitchProp; + screenProps: ScreenProps; + }; + + export type NavigationSwitchScreenComponent< + Params = NavigationParams, + ScreenProps = unknown + > = React.ComponentType> & { + navigationOptions?: NavigationScreenConfig< + {}, + NavigationSwitchProp, + ScreenProps + >; + }; + export type NavigationStackAction = | NavigationInitAction | NavigationNavigateAction @@ -437,9 +487,17 @@ declare module 'react-navigation' { | NavigationComponent | ( | { screen: NavigationComponent } - | { getScreen(): NavigationScreenComponent }) & { - navigationOptions?: NavigationScreenConfig; - params?: { [key: string]: any } + | { + getScreen(): NavigationScreenComponent< + Options, + NavigationScreenPropType + >; + }) & { + navigationOptions?: NavigationScreenConfig< + Options, + NavigationScreenPropType + >; + params?: { [key: string]: any }; path?: string; }; @@ -458,7 +516,10 @@ declare module 'react-navigation' { } export interface NavigationRouteConfigMap { - [routeName: string]: NavigationRouteConfig; + [routeName: string]: NavigationRouteConfig< + Options, + NavigationScreenPropType + >; } export type NavigationDispatch = (action: NavigationAction) => boolean; @@ -542,7 +603,11 @@ declare module 'react-navigation' { dangerouslyGetParent: () => NavigationScreenProp | undefined; } - export interface NavigationNavigatorProps { + export interface NavigationNavigatorProps< + Options = {}, + State = {}, + ScreenProps = unknown + > { theme?: SupportedThemes | 'no-preference'; detached?: boolean; navigation?: NavigationProp; @@ -555,7 +620,11 @@ declare module 'react-navigation' { | 'react-navigation/TABS' | 'react-navigation/DRAWER'; - export interface NavigationContainerProps { + export interface NavigationContainerProps< + State = {}, + Options = {}, + ScreenProps = unknown + > { uriPrefix?: string | RegExp; /** * Controls whether the navigation container handles URLs opened via 'Linking' @@ -614,7 +683,10 @@ declare module 'react-navigation' { } export function createSwitchNavigator( - routeConfigMap: NavigationRouteConfigMap>, + routeConfigMap: NavigationRouteConfigMap< + SwitchNavigatorConfig, + NavigationScreenProp + >, switchConfig?: SwitchNavigatorConfig ): NavigationNavigator<{}, NavigationProp>; @@ -735,7 +807,11 @@ declare module 'react-navigation' { getComponent: () => React.ComponentType; } - export type NavigationView = React.ComponentType< + export type NavigationView< + Options, + State, + ScreenProps = unknown + > = React.ComponentType< { descriptors: { [key: string]: NavigationDescriptor }; navigationConfig: Options; @@ -743,10 +819,22 @@ declare module 'react-navigation' { } & NavigationInjectedProps >; - export type CreateNavigatorConfig = NavigatorConfig & RouterConfig & { - defaultNavigationOptions?: NavigationScreenConfig; - navigationOptions?: NavigationScreenConfig<{ [key: string]: any }, NavigationScreenProp>; - } + export type CreateNavigatorConfig< + NavigatorConfig, + RouterConfig, + Options, + NavigationScreenPropType + > = NavigatorConfig & + RouterConfig & { + defaultNavigationOptions?: NavigationScreenConfig< + Options, + NavigationScreenPropType + >; + navigationOptions?: NavigationScreenConfig< + { [key: string]: any }, + NavigationScreenProp + >; + }; export function createNavigator( view: NavigationView,