refactor: tweak type for screen options

This commit is contained in:
Satyajit Sahoo
2020-06-15 18:12:08 +02:00
parent 07c30b2847
commit 33476b9cb5
7 changed files with 27 additions and 41 deletions

View File

@@ -15,7 +15,7 @@ import { NavigationProp, RouteConfig, EventMapBase } from './types';
type Props<
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
> = {
screen: RouteConfig<ParamListBase, string, State, ScreenOptions, EventMap>;
@@ -34,7 +34,7 @@ type Props<
*/
export default function SceneView<
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
>({
screen,

View File

@@ -8,7 +8,7 @@ export default function Screen<
ParamList extends ParamListBase,
RouteName extends keyof ParamList,
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
>(_: RouteConfig<ParamList, RouteName, State, ScreenOptions, EventMap>) {
/* istanbul ignore next */

View File

@@ -12,7 +12,7 @@ import { TypedNavigator, EventMapBase } from './types';
*/
export default function createNavigatorFactory<
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase,
NavigatorComponent extends React.ComponentType<any>
>(Navigator: NavigatorComponent) {

View File

@@ -10,7 +10,7 @@ import {
} from '@react-navigation/routers';
export type DefaultNavigatorOptions<
ScreenOptions extends object
ScreenOptions extends {}
> = DefaultRouterOptions & {
/**
* Children React Elements to extract the route configuration from.
@@ -37,7 +37,6 @@ export type EventMapCore<State extends NavigationState> = {
focus: { data: undefined };
blur: { data: undefined };
state: { data: { state: State } };
options: { data: { options: object } };
};
export type EventArg<
@@ -251,7 +250,7 @@ export type NavigationProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
State extends NavigationState = NavigationState,
ScreenOptions extends object = {},
ScreenOptions extends {} = {},
EventMap extends EventMapBase = {}
> = NavigationHelpersCommon<ParamList, State> & {
/**
@@ -322,7 +321,7 @@ export type Descriptor<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string,
State extends NavigationState = NavigationState,
ScreenOptions extends object = {},
ScreenOptions extends {} = {},
EventMap extends EventMapBase = {}
> = {
/**
@@ -361,7 +360,7 @@ export type RouteConfig<
ParamList extends ParamListBase,
RouteName extends keyof ParamList,
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
> = {
/**
@@ -471,7 +470,7 @@ export type NavigationContainerRef = NavigationHelpers<ParamListBase> &
export type TypedNavigator<
ParamList extends ParamListBase,
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase,
Navigator extends React.ComponentType<any>
> = {
@@ -479,29 +478,14 @@ export type TypedNavigator<
* Navigator component which manages the child screens.
*/
Navigator: React.ComponentType<
Omit<
React.ComponentProps<Navigator>,
'initialRouteName' | 'screenOptions'
> & {
/**
* Name of the route to focus by on initial render.
* If not specified, usually the first route is used.
*/
initialRouteName?: keyof ParamList;
/**
* Default options for all screens under this navigator.
*/
screenOptions?:
| ScreenOptions
| ((props: {
route: RouteProp<ParamList, keyof ParamList>;
navigation: any;
}) => ScreenOptions);
/**
* Configuration for screens
*/
children: React.ReactNode;
}
Omit<React.ComponentProps<Navigator>, keyof DefaultNavigatorOptions<any>> &
Omit<DefaultNavigatorOptions<ScreenOptions>, 'initialRouteName'> & {
/**
* Name of the route to focus by on initial render.
* If not specified, usually the first route is used.
*/
initialRouteName?: keyof ParamList;
}
>;
/**
* Component used for specifying route configuration.

View File

@@ -25,7 +25,7 @@ import NavigationRouteContext from './NavigationRouteContext';
type Options<
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
> = {
state: State;
@@ -64,7 +64,7 @@ type Options<
*/
export default function useDescriptors<
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
>({
state,
@@ -128,7 +128,8 @@ export default function useDescriptors<
// The default `screenOptions` passed to the navigator
...(typeof screenOptions === 'object' || screenOptions == null
? screenOptions
: screenOptions({
: // @ts-ignore: this is a function, but typescript doesn't think so
screenOptions({
// @ts-ignore
route,
navigation,
@@ -136,7 +137,8 @@ export default function useDescriptors<
// The `options` prop passed to `Screen` elements
...(typeof screen.options === 'object' || screen.options == null
? screen.options
: screen.options({
: // @ts-ignore: this is a function, but typescript doesn't think so
screen.options({
// @ts-ignore
route,
// @ts-ignore

View File

@@ -56,7 +56,7 @@ type NavigatorRoute = {
*/
const getRouteConfigsFromChildren = <
State extends NavigationState,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends EventMapBase
>(
children: React.ReactNode
@@ -160,7 +160,7 @@ const getRouteConfigsFromChildren = <
export default function useNavigationBuilder<
State extends NavigationState,
RouterOptions extends DefaultRouterOptions,
ScreenOptions extends object,
ScreenOptions extends {},
EventMap extends Record<string, any>
>(
createRouter: RouterFactory<State, any, RouterOptions>,

View File

@@ -24,7 +24,7 @@ type Options<State extends NavigationState> = {
type NavigationCache<
State extends NavigationState,
ScreenOptions extends object
ScreenOptions extends {}
> = {
[key: string]: NavigationProp<ParamListBase, string, State, ScreenOptions>;
};
@@ -36,7 +36,7 @@ type NavigationCache<
*/
export default function useNavigationCache<
State extends NavigationState,
ScreenOptions extends object
ScreenOptions extends {}
>({
state,
getState,