From 692a796e076a1d05cb7c7b565c39fdd2b0632a52 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Tue, 23 Jul 2019 00:53:38 +0200 Subject: [PATCH] fix: fix type signature for setParams (#24) --- .gitignore | 1 + example/StackNavigator.tsx | 5 +++-- example/TabNavigator.tsx | 8 +++---- example/index.tsx | 14 ++++++------ src/NavigationContext.tsx | 4 ++-- src/SceneView.tsx | 4 ++-- src/types.tsx | 41 ++++++++++++++++++++++-------------- src/useDescriptors.tsx | 4 ++-- src/useNavigationHelpers.tsx | 4 ++-- 9 files changed, 48 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 6b7e3c23..723d27f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store .cache .vscode +.idea /coverage/ /types/ diff --git a/example/StackNavigator.tsx b/example/StackNavigator.tsx index 1adda2bb..a5864568 100644 --- a/example/StackNavigator.tsx +++ b/example/StackNavigator.tsx @@ -36,8 +36,9 @@ export type StackNavigationOptions = { }; export type StackNavigationProp< - ParamList extends ParamListBase -> = NavigationProp & { + ParamList extends ParamListBase, + RouteName extends keyof ParamList = string +> = NavigationProp & { /** * Push a new screen onto the stack. * diff --git a/example/TabNavigator.tsx b/example/TabNavigator.tsx index 45a817df..a9edf514 100644 --- a/example/TabNavigator.tsx +++ b/example/TabNavigator.tsx @@ -29,10 +29,10 @@ export type TabNavigationOptions = { title?: string; }; -export type TabNavigationProp = NavigationProp< - ParamList, - TabNavigationOptions -> & { +export type TabNavigationProp< + ParamList extends ParamListBase, + RouteName extends keyof ParamList = string +> = NavigationProp & { /** * Jump to an existing tab. * diff --git a/example/index.tsx b/example/index.tsx index 00b62459..3254eb9e 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -3,7 +3,7 @@ import { render } from 'react-dom'; import { NavigationContainer, CompositeNavigationProp, - NavigationProp, + NavigationHelpers, RouteProp, InitialState, } from '../src'; @@ -30,8 +30,8 @@ const First = ({ route, }: { navigation: CompositeNavigationProp< - StackNavigationProp, - NavigationProp + StackNavigationProp, + NavigationHelpers >; route: RouteProp; }) => ( @@ -62,8 +62,8 @@ const Second = ({ navigation, }: { navigation: CompositeNavigationProp< - StackNavigationProp, - NavigationProp + StackNavigationProp, + NavigationHelpers >; }) => { const [count, setCount] = React.useState(0); @@ -98,7 +98,7 @@ const Fourth = ({ navigation, }: { navigation: CompositeNavigationProp< - TabNavigationProp, + TabNavigationProp, StackNavigationProp >; }) => ( @@ -123,7 +123,7 @@ const Fifth = ({ navigation, }: { navigation: CompositeNavigationProp< - TabNavigationProp, + TabNavigationProp, StackNavigationProp >; }) => ( diff --git a/src/NavigationContext.tsx b/src/NavigationContext.tsx index 65f5bd02..e066e459 100644 --- a/src/NavigationContext.tsx +++ b/src/NavigationContext.tsx @@ -1,8 +1,8 @@ import * as React from 'react'; -import { NavigationProp, ParamListBase } from './types'; +import { NavigationHelpers, ParamListBase } from './types'; const NavigationContext = React.createContext< - NavigationProp | undefined + NavigationHelpers | undefined >(undefined); export default NavigationContext; diff --git a/src/SceneView.tsx b/src/SceneView.tsx index 48f5b138..4abccc7a 100644 --- a/src/SceneView.tsx +++ b/src/SceneView.tsx @@ -7,14 +7,14 @@ import { Route, ParamListBase, NavigationState, - NavigationProp, + NavigationHelpers, RouteConfig, TargetRoute, } from './types'; type Props = { screen: RouteConfig; - navigation: NavigationProp; + navigation: NavigationHelpers; route: Route & { state?: NavigationState }; getState: () => NavigationState; setState: (state: NavigationState) => void; diff --git a/src/types.tsx b/src/types.tsx index 0b27813d..9aab34a6 100644 --- a/src/types.tsx +++ b/src/types.tsx @@ -162,10 +162,7 @@ class PrivateValueStore { private __private_value_type?: T; } -export type NavigationProp< - ParamList extends ParamListBase, - ScreenOptions extends object = {} -> = { +export type NavigationHelpers = { /** * Dispatch an action or an update function to the router. * The update function will receive the current state, @@ -218,12 +215,30 @@ export type NavigationProp< * The new params will be shallow merged with the old one. * * @param params Params object for the current route. - * @routeName params Target route for setParam. + * @param target Target route for updating params. */ setParams>( params: ParamList[RouteName], target: TargetRoute ): void; +} & PrivateValueStore; + +export type NavigationProp< + ParamList extends ParamListBase, + RouteName extends keyof ParamList = string, + ScreenOptions extends object = {} +> = Omit, 'setParams'> & { + /** + * Update the param object for the route. + * The new params will be shallow merged with the old one. + * + * @param params Params object for the current route. + * @param [target] Target route for updating params. Defaults to current route. + */ + setParams( + params: ParamList[TargetRouteName], + target?: TargetRoute> + ): void; /** * Update the options for the route. @@ -232,7 +247,7 @@ export type NavigationProp< * @param options Options object for the route. */ setOptions(options: Partial): void; -} & PrivateValueStore; +}; export type RouteProp< ParamList extends ParamListBase, @@ -248,15 +263,9 @@ export type RouteProp< }); export type CompositeNavigationProp< - A extends NavigationProp, - B extends NavigationProp -> = Omit> & - NavigationProp< - (A extends NavigationProp ? T : never) & - (B extends NavigationProp ? U : never), - (A extends NavigationProp ? O : never) & - (B extends NavigationProp ? P : never) - >; + A extends Omit, 'setParams'>, + B extends Omit, 'setParams'> +> = A & B; export type Descriptor = { /** @@ -287,7 +296,7 @@ export type RouteConfig< | ScreenOptions | ((props: { route: RouteProp; - navigation: NavigationProp; + navigation: NavigationHelpers; }) => ScreenOptions); /** diff --git a/src/useDescriptors.tsx b/src/useDescriptors.tsx index 34017056..174bb597 100644 --- a/src/useDescriptors.tsx +++ b/src/useDescriptors.tsx @@ -3,7 +3,7 @@ import { Descriptor, PartialState, NavigationAction, - NavigationProp, + NavigationHelpers, NavigationState, ParamListBase, RouteConfig, @@ -16,7 +16,7 @@ import NavigationBuilderContext, { type Options = { state: NavigationState | PartialState; screens: { [key: string]: RouteConfig }; - navigation: NavigationProp; + navigation: NavigationHelpers; onAction: (action: NavigationAction, sourceNavigatorKey?: string) => boolean; getState: () => NavigationState; setState: (state: NavigationState) => void; diff --git a/src/useNavigationHelpers.tsx b/src/useNavigationHelpers.tsx index c7801564..a75ab99c 100644 --- a/src/useNavigationHelpers.tsx +++ b/src/useNavigationHelpers.tsx @@ -3,7 +3,7 @@ import * as BaseActions from './BaseActions'; import NavigationContext from './NavigationContext'; import { NavigationStateContext } from './NavigationContainer'; import { - NavigationProp, + NavigationHelpers, NavigationAction, NavigationState, ActionCreators, @@ -26,7 +26,7 @@ export default function useNavigationHelpers({ const parentNavigationHelpers = React.useContext(NavigationContext); const { performTransaction } = React.useContext(NavigationStateContext); - return React.useMemo((): NavigationProp => { + return React.useMemo((): NavigationHelpers => { const dispatch = ( action: NavigationAction | ((state: NavigationState) => NavigationState) ) => {