mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-12 22:51:18 +08:00
fix: fix type signature for setParams (#24)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
.DS_Store
|
||||
.cache
|
||||
.vscode
|
||||
.idea
|
||||
|
||||
/coverage/
|
||||
/types/
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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<StackParamList>,
|
||||
NavigationProp<TabParamList>
|
||||
StackNavigationProp<StackParamList, 'first'>,
|
||||
NavigationHelpers<TabParamList>
|
||||
>;
|
||||
route: RouteProp<StackParamList, 'first'>;
|
||||
}) => (
|
||||
@@ -62,8 +62,8 @@ const Second = ({
|
||||
navigation,
|
||||
}: {
|
||||
navigation: CompositeNavigationProp<
|
||||
StackNavigationProp<StackParamList>,
|
||||
NavigationProp<TabParamList>
|
||||
StackNavigationProp<StackParamList, 'second'>,
|
||||
NavigationHelpers<TabParamList>
|
||||
>;
|
||||
}) => {
|
||||
const [count, setCount] = React.useState(0);
|
||||
@@ -98,7 +98,7 @@ const Fourth = ({
|
||||
navigation,
|
||||
}: {
|
||||
navigation: CompositeNavigationProp<
|
||||
TabNavigationProp<TabParamList>,
|
||||
TabNavigationProp<TabParamList, 'fourth'>,
|
||||
StackNavigationProp<StackParamList>
|
||||
>;
|
||||
}) => (
|
||||
@@ -123,7 +123,7 @@ const Fifth = ({
|
||||
navigation,
|
||||
}: {
|
||||
navigation: CompositeNavigationProp<
|
||||
TabNavigationProp<TabParamList>,
|
||||
TabNavigationProp<TabParamList, 'fifth'>,
|
||||
StackNavigationProp<StackParamList>
|
||||
>;
|
||||
}) => (
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
@@ -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> = {
|
||||
/**
|
||||
@@ -287,7 +296,7 @@ export type RouteConfig<
|
||||
| ScreenOptions
|
||||
| ((props: {
|
||||
route: RouteProp<ParamList, RouteName>;
|
||||
navigation: NavigationProp<ParamList, ScreenOptions>;
|
||||
navigation: NavigationHelpers<ParamList>;
|
||||
}) => ScreenOptions);
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
Descriptor,
|
||||
PartialState,
|
||||
NavigationAction,
|
||||
NavigationProp,
|
||||
NavigationHelpers,
|
||||
NavigationState,
|
||||
ParamListBase,
|
||||
RouteConfig,
|
||||
@@ -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;
|
||||
|
||||
@@ -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<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)
|
||||
) => {
|
||||
|
||||
Reference in New Issue
Block a user