Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: fix type signature for setParams (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 authored Jul 22, 2019
1 parent 83264a3 commit 692a796
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.cache
.vscode
.idea

/coverage/
/types/
Expand Down
5 changes: 3 additions & 2 deletions example/StackNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export type StackNavigationOptions = {
};

export type StackNavigationProp<
ParamList extends ParamListBase
> = NavigationProp<ParamList, StackNavigationOptions> & {
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string
> = NavigationProp<ParamList, RouteName, StackNavigationOptions> & {
/**
* Push a new screen onto the stack.
*
Expand Down
8 changes: 4 additions & 4 deletions example/TabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export type TabNavigationOptions = {
title?: string;
};

export type TabNavigationProp<ParamList extends ParamListBase> = NavigationProp<
ParamList,
TabNavigationOptions
> & {
export type TabNavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string
> = NavigationProp<ParamList, RouteName, TabNavigationOptions> & {
/**
* Jump to an existing tab.
*
Expand Down
14 changes: 7 additions & 7 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render } from 'react-dom';
import {
NavigationContainer,
CompositeNavigationProp,
NavigationProp,
NavigationHelpers,
RouteProp,
InitialState,
} from '../src';
Expand All @@ -30,8 +30,8 @@ const First = ({
route,
}: {
navigation: CompositeNavigationProp<
StackNavigationProp<StackParamList>,
NavigationProp<TabParamList>
StackNavigationProp<StackParamList, 'first'>,
NavigationHelpers<TabParamList>
>;
route: RouteProp<StackParamList, 'first'>;
}) => (
Expand Down Expand Up @@ -62,8 +62,8 @@ const Second = ({
navigation,
}: {
navigation: CompositeNavigationProp<
StackNavigationProp<StackParamList>,
NavigationProp<TabParamList>
StackNavigationProp<StackParamList, 'second'>,
NavigationHelpers<TabParamList>
>;
}) => {
const [count, setCount] = React.useState(0);
Expand Down Expand Up @@ -98,7 +98,7 @@ const Fourth = ({
navigation,
}: {
navigation: CompositeNavigationProp<
TabNavigationProp<TabParamList>,
TabNavigationProp<TabParamList, 'fourth'>,
StackNavigationProp<StackParamList>
>;
}) => (
Expand All @@ -123,7 +123,7 @@ const Fifth = ({
navigation,
}: {
navigation: CompositeNavigationProp<
TabNavigationProp<TabParamList>,
TabNavigationProp<TabParamList, 'fifth'>,
StackNavigationProp<StackParamList>
>;
}) => (
Expand Down
4 changes: 2 additions & 2 deletions src/NavigationContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { NavigationProp, ParamListBase } from './types';
import { NavigationHelpers, ParamListBase } from './types';

const NavigationContext = React.createContext<
NavigationProp<ParamListBase> | undefined
NavigationHelpers<ParamListBase> | undefined
>(undefined);

export default NavigationContext;
4 changes: 2 additions & 2 deletions src/SceneView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
Route,
ParamListBase,
NavigationState,
NavigationProp,
NavigationHelpers,
RouteConfig,
TargetRoute,
} from './types';

type Props<ScreenOptions extends object> = {
screen: RouteConfig<ParamListBase, string, ScreenOptions>;
navigation: NavigationProp<ParamListBase>;
navigation: NavigationHelpers<ParamListBase>;
route: Route<string> & { state?: NavigationState };
getState: () => NavigationState;
setState: (state: NavigationState) => void;
Expand Down
41 changes: 25 additions & 16 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ class PrivateValueStore<T> {
private __private_value_type?: T;
}

export type NavigationProp<
ParamList extends ParamListBase,
ScreenOptions extends object = {}
> = {
export type NavigationHelpers<ParamList extends ParamListBase> = {
/**
* Dispatch an action or an update function to the router.
* The update function will receive the current state,
Expand Down Expand Up @@ -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<RouteName extends Extract<keyof ParamList, string>>(
params: ParamList[RouteName],
target: TargetRoute<RouteName>
): void;
} & PrivateValueStore<ParamList>;

export type NavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
ScreenOptions extends object = {}
> = Omit<NavigationHelpers<ParamList>, '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<TargetRouteName extends keyof ParamList = RouteName>(
params: ParamList[TargetRouteName],
target?: TargetRoute<Extract<TargetRouteName, string>>
): void;

/**
* Update the options for the route.
Expand All @@ -232,7 +247,7 @@ export type NavigationProp<
* @param options Options object for the route.
*/
setOptions(options: Partial<ScreenOptions>): void;
} & PrivateValueStore<ParamList>;
};

export type RouteProp<
ParamList extends ParamListBase,
Expand All @@ -248,15 +263,9 @@ export type RouteProp<
});

export type CompositeNavigationProp<
A extends NavigationProp<ParamListBase, object>,
B extends NavigationProp<ParamListBase, object>
> = Omit<A & B, keyof NavigationProp<any, any>> &
NavigationProp<
(A extends NavigationProp<infer T, any> ? T : never) &
(B extends NavigationProp<infer U, any> ? U : never),
(A extends NavigationProp<any, infer O> ? O : never) &
(B extends NavigationProp<any, infer P> ? P : never)
>;
A extends Omit<NavigationHelpers<ParamListBase>, 'setParams'>,
B extends Omit<NavigationHelpers<ParamListBase>, 'setParams'>
> = A & B;

export type Descriptor<ScreenOptions extends object> = {
/**
Expand Down Expand Up @@ -287,7 +296,7 @@ export type RouteConfig<
| ScreenOptions
| ((props: {
route: RouteProp<ParamList, RouteName>;
navigation: NavigationProp<ParamList, ScreenOptions>;
navigation: NavigationHelpers<ParamList>;
}) => ScreenOptions);

/**
Expand Down
4 changes: 2 additions & 2 deletions src/useDescriptors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Descriptor,
PartialState,
NavigationAction,
NavigationProp,
NavigationHelpers,
NavigationState,
ParamListBase,
RouteConfig,
Expand All @@ -16,7 +16,7 @@ import NavigationBuilderContext, {
type Options<ScreenOptions extends object> = {
state: NavigationState | PartialState;
screens: { [key: string]: RouteConfig<ParamListBase, string, ScreenOptions> };
navigation: NavigationProp<ParamListBase>;
navigation: NavigationHelpers<ParamListBase>;
onAction: (action: NavigationAction, sourceNavigatorKey?: string) => boolean;
getState: () => NavigationState;
setState: (state: NavigationState) => void;
Expand Down
4 changes: 2 additions & 2 deletions src/useNavigationHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as BaseActions from './BaseActions';
import NavigationContext from './NavigationContext';
import { NavigationStateContext } from './NavigationContainer';
import {
NavigationProp,
NavigationHelpers,
NavigationAction,
NavigationState,
ActionCreators,
Expand All @@ -26,7 +26,7 @@ export default function useNavigationHelpers<Action extends NavigationAction>({
const parentNavigationHelpers = React.useContext(NavigationContext);
const { performTransaction } = React.useContext(NavigationStateContext);

return React.useMemo((): NavigationProp<ParamListBase> => {
return React.useMemo((): NavigationHelpers<ParamListBase> => {
const dispatch = (
action: NavigationAction | ((state: NavigationState) => NavigationState)
) => {
Expand Down

0 comments on commit 692a796

Please sign in to comment.