From 732040440df7b3b35244b09e24c893a9151dfd0e Mon Sep 17 00:00:00 2001 From: Alec Hill Date: Mon, 29 Oct 2018 12:25:51 +0000 Subject: [PATCH 1/2] Injected navigation props were incorrectly typed to provide only non-leaf route state props. Added tests for both kinds of injected route state. --- types/react-navigation/index.d.ts | 3 +- .../react-navigation-tests.tsx | 41 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/types/react-navigation/index.d.ts b/types/react-navigation/index.d.ts index 1bdf43bc35..4be9671d66 100644 --- a/types/react-navigation/index.d.ts +++ b/types/react-navigation/index.d.ts @@ -25,6 +25,7 @@ // Denis Frezzato // Mickael Wegerich // Max Davidson +// Alec Hill // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -1240,7 +1241,7 @@ export type Omit = Pick>; export type InferProps> = T extends React.ComponentType ? P : never; export interface NavigationInjectedProps

{ - navigation: NavigationScreenProp; + navigation: NavigationScreenProp, P>; } // If the wrapped component is a class, we can get a ref to it diff --git a/types/react-navigation/react-navigation-tests.tsx b/types/react-navigation/react-navigation-tests.tsx index 47dd7baa0e..d2ef1d522c 100644 --- a/types/react-navigation/react-navigation-tests.tsx +++ b/types/react-navigation/react-navigation-tests.tsx @@ -17,12 +17,14 @@ import { NavigationNavigateAction, NavigationProp, NavigationResetAction, + NavigationRoute, NavigationRouteConfigMap, NavigationScreenProp, NavigationScreenProps, NavigationSetParamsAction, NavigationStackAction, NavigationStackScreenOptions, + NavigationStateRoute, NavigationTabScreenOptions, NavigationTransitionProps, StackViewTransitionConfigs, @@ -70,10 +72,22 @@ interface StartScreenNavigationParams { */ class StartScreen extends React.Component> { render() { - // Implicit type checks. + // Injected type checks + const props: NavigationInjectedProps = this.props; + // route state... + const navigationState: NavigationRoute = this.props.navigation.state; + const index: number = navigationState.index; + const key: string = navigationState.key; + const routeName: string = navigationState.routeName; + const path: string | undefined = navigationState.path; + let routes: NavigationRoute[]; + if (isNavigationStateRoute(navigationState)) { + routes = navigationState.routes; + } + // params... const navigationStateParams: StartScreenNavigationParams | undefined = this.props.navigation.state.params; - const id = this.props.navigation.state.params && this.props.navigation.state.params.id; - const s = this.props.navigation.state.params && this.props.navigation.state.params.s; + const id: number | undefined = this.props.navigation.state.params && this.props.navigation.state.params.id; + const s: string | undefined = this.props.navigation.state.params && this.props.navigation.state.params.s; return ( @@ -107,17 +121,32 @@ interface NextScreenNavigationParams { class NextScreen extends React.Component> { render() { - // Implicit type checks. - const navigationStateParams: NextScreenNavigationParams | undefined = this.props.navigation.state.params; + // Injected type checks + const props: NavigationInjectedProps = this.props; + // route state... + const navigationState: NavigationRoute = this.props.navigation.state; + const index: number = navigationState.index; + const key: string = navigationState.key; + const routeName: string = navigationState.routeName; + const path: string | undefined = navigationState.path; + let routes: NavigationRoute[]; + if (isNavigationStateRoute(navigationState)) { + routes = navigationState.routes; + } + // params... + const navigationStateParams: NextScreenNavigationParams | undefined = navigationState.params; const id = this.props.navigation.state.params && this.props.navigation.state.params.id; const name = this.props.navigation.getParam('name', 'Peter'); - return ( ); } } +function isNavigationStateRoute

(route: NavigationRoute

): route is NavigationStateRoute

{ + return !!(route as NavigationStateRoute

).routes; +} + const navigationOptions = { headerBackTitle: null, }; From 6acfee7450ecb3a69e9c9827d337717562363f1a Mon Sep 17 00:00:00 2001 From: Alec Hill Date: Mon, 29 Oct 2018 15:47:20 +0000 Subject: [PATCH 2/2] Removed Slessi from authors on request --- types/react-navigation/index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/react-navigation/index.d.ts b/types/react-navigation/index.d.ts index 4be9671d66..3562072601 100644 --- a/types/react-navigation/index.d.ts +++ b/types/react-navigation/index.d.ts @@ -14,7 +14,6 @@ // Steven Miller // Armando Assuncao // Ciaran Liedeman -// Edward Sammut Alessi // Jérémy Magrin // Luca Campana // Ullrich Schaefer