Injected navigation props were incorrectly typed to provide only non-leaf route state props. Added tests for both kinds of injected route state.

This commit is contained in:
Alec Hill
2018-10-29 12:25:51 +00:00
parent 3d82362c03
commit 732040440d
2 changed files with 37 additions and 7 deletions
+2 -1
View File
@@ -25,6 +25,7 @@
// Denis Frezzato <https://github.com/DenisFrezzato>
// Mickael Wegerich <https://github.com/mickaelw>
// Max Davidson <https://github.com/maxdavidson>
// Alec Hill <https://github.com/alechill>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -1240,7 +1241,7 @@ export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
export type InferProps<T extends React.ComponentType<any>> = T extends React.ComponentType<infer P> ? P : never;
export interface NavigationInjectedProps<P = NavigationParams> {
navigation: NavigationScreenProp<NavigationState, P>;
navigation: NavigationScreenProp<NavigationRoute<P>, P>;
}
// If the wrapped component is a class, we can get a ref to it
@@ -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<NavigationScreenProps<StartScreenNavigationParams>> {
render() {
// Implicit type checks.
// Injected type checks
const props: NavigationInjectedProps<StartScreenNavigationParams> = this.props;
// route state...
const navigationState: NavigationRoute<StartScreenNavigationParams> = 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 (
<View>
@@ -107,17 +121,32 @@ interface NextScreenNavigationParams {
class NextScreen extends React.Component<NavigationScreenProps<NextScreenNavigationParams>> {
render() {
// Implicit type checks.
const navigationStateParams: NextScreenNavigationParams | undefined = this.props.navigation.state.params;
// Injected type checks
const props: NavigationInjectedProps<NextScreenNavigationParams> = this.props;
// route state...
const navigationState: NavigationRoute<NextScreenNavigationParams> = 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 (
<View />
);
}
}
function isNavigationStateRoute<P>(route: NavigationRoute<P>): route is NavigationStateRoute<P> {
return !!(route as NavigationStateRoute<P>).routes;
}
const navigationOptions = {
headerBackTitle: null,
};