fix: more improvements to types

This commit is contained in:
Satyajit Sahoo
2020-06-17 13:46:21 +02:00
parent 962456beb6
commit d2444887be
12 changed files with 36 additions and 30 deletions

View File

@@ -16,10 +16,6 @@ type EventName =
| 'didBlur'
| 'refocus';
// const focusSubscriptions = new WeakMap<() => void, () => void>();
// const blurSubscriptions = new WeakMap<() => void, () => void>();
// const refocusSubscriptions = new WeakMap<() => void, () => void>();
export default function createCompatNavigationProp<
NavigationPropType extends NavigationProp<ParamListBase>,
ParamList extends ParamListBase = NavigationPropType extends NavigationProp<

View File

@@ -6,7 +6,6 @@ import {
TypedNavigator,
NavigationProp,
RouteProp,
EventMapBase,
NavigationRouteContext,
} from '@react-navigation/native';
import CompatScreen from './CompatScreen';
@@ -19,7 +18,7 @@ export default function createCompatNavigatorFactory<
ParamListBase,
NavigationState,
{},
EventMapBase,
any,
React.ComponentType<any>
>
>(createNavigator: CreateNavigator) {
@@ -116,7 +115,8 @@ export default function createCompatNavigatorFactory<
typeof screenNavigationOptions === 'function'
? {
navigation: createCompatNavigationProp<
NavigationPropType
NavigationPropType,
ParamList
>(navigation, route, {}),
navigationOptions: defaultNavigationOptions || {},
screenProps,

View File

@@ -17,8 +17,11 @@ import useOptionsGetters from './useOptionsGetters';
import useEventEmitter from './useEventEmitter';
import useSyncState from './useSyncState';
import isSerializable from './isSerializable';
import type { NavigationContainerRef, NavigationContainerProps } from './types';
import type {
NavigationContainerEventMap,
NavigationContainerRef,
NavigationContainerProps,
} from './types';
type State = NavigationState | PartialState<NavigationState> | undefined;
@@ -175,7 +178,7 @@ const BaseNavigationContainer = React.forwardRef(
return state.routes[state.index];
}, [getRootState]);
const emitter = useEventEmitter();
const emitter = useEventEmitter<NavigationContainerEventMap>();
const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({});

View File

@@ -61,7 +61,7 @@ export type EventArg<
preventDefault(): void;
}
: {}) &
(Data extends undefined ? {} : { readonly data: Data });
(undefined extends Data ? {} : { readonly data: Data });
export type EventListenerCallback<
EventMap extends EventMapBase,
@@ -107,7 +107,7 @@ export type EventEmitter<EventMap extends EventMapBase> = {
} & (EventMap[EventName]['canPreventDefault'] extends true
? { canPreventDefault: true }
: {}) &
(EventMap[EventName]['data'] extends undefined
(undefined extends EventMap[EventName]['data']
? {}
: { data: EventMap[EventName]['data'] })
): EventArg<
@@ -275,7 +275,7 @@ export type RouteProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList
> = Omit<Route<Extract<RouteName, string>>, 'params'> &
(ParamList[RouteName] extends undefined
(undefined extends ParamList[RouteName]
? {}
: {
/**
@@ -419,7 +419,7 @@ export type NavigationContainerEventMap = {
/**
* The updated state object after the state change.
*/
state: NavigationState;
state: NavigationState | PartialState<NavigationState> | undefined;
};
};
/**

View File

@@ -51,7 +51,7 @@ type Options<
addStateGetter: (key: string, getter: NavigatorStateGetter) => void;
onRouteFocus: (key: string) => void;
router: Router<State, NavigationAction>;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<any>;
};
/**

View File

@@ -1,8 +1,10 @@
import * as React from 'react';
import type { EventEmitter, EventConsumer, EventArg } from './types';
export type NavigationEventEmitter = EventEmitter<Record<string, any>> & {
create: (target: string) => EventConsumer<Record<string, any>>;
export type NavigationEventEmitter<
T extends Record<string, any>
> = EventEmitter<T> & {
create: (target: string) => EventConsumer<T>;
};
type Listeners = ((e: any) => void)[];
@@ -10,9 +12,9 @@ type Listeners = ((e: any) => void)[];
/**
* Hook to manage the event system used by the navigator to notify screens of various events.
*/
export default function useEventEmitter(
export default function useEventEmitter<T extends Record<string, any>>(
listen?: (e: any) => void
): NavigationEventEmitter {
): NavigationEventEmitter<T> {
const listenRef = React.useRef(listen);
React.useEffect(() => {

View File

@@ -2,16 +2,20 @@ import * as React from 'react';
import type { NavigationState } from '@react-navigation/routers';
import NavigationContext from './NavigationContext';
import type { NavigationEventEmitter } from './useEventEmitter';
import type { EventMapCore } from './types';
type Options = {
state: NavigationState;
emitter: NavigationEventEmitter;
type Options<State extends NavigationState> = {
state: State;
emitter: NavigationEventEmitter<EventMapCore<State>>;
};
/**
* Hook to take care of emitting `focus` and `blur` events.
*/
export default function useFocusEvents({ state, emitter }: Options) {
export default function useFocusEvents<State extends NavigationState>({
state,
emitter,
}: Options<State>) {
const navigation = React.useContext(NavigationContext);
const lastFocusedKeyRef = React.useRef<string | undefined>();

View File

@@ -29,6 +29,7 @@ import {
RouteConfig,
PrivateValueStore,
EventMapBase,
EventMapCore,
} from './types';
import useStateGetters from './useStateGetters';
import useOnGetState from './useOnGetState';
@@ -378,7 +379,7 @@ export default function useNavigationBuilder<
: (initializedStateRef.current as State);
}, [getCurrentState, isStateInitialized]);
const emitter = useEventEmitter((e) => {
const emitter = useEventEmitter<EventMapCore<State>>((e) => {
let routeNames = [];
let route: Route<string> | undefined;

View File

@@ -8,7 +8,7 @@ import {
} from '@react-navigation/routers';
import type { NavigationEventEmitter } from './useEventEmitter';
import type { NavigationHelpers, NavigationProp } from './types';
import type { EventMapBase, NavigationHelpers, NavigationProp } from './types';
type Options<State extends NavigationState> = {
state: State;
@@ -19,7 +19,7 @@ type Options<State extends NavigationState> = {
cb: (options: Record<string, object>) => Record<string, object>
) => void;
router: Router<State, NavigationAction>;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<EventMapBase>;
};
type NavigationCache<

View File

@@ -20,7 +20,7 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
visitedNavigators?: Set<string>
) => boolean;
getState: () => State;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<any>;
router: Router<State, Action>;
};

View File

@@ -51,7 +51,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the new route.
*/
replace<RouteName extends keyof ParamList>(
...args: ParamList[RouteName] extends undefined
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;
@@ -63,7 +63,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the route.
*/
push<RouteName extends keyof ParamList>(
...args: ParamList[RouteName] extends undefined | any
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;

View File

@@ -42,7 +42,7 @@ export type TabActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the route.
*/
jumpTo<RouteName extends Extract<keyof ParamList, string>>(
...args: ParamList[RouteName] extends undefined | any
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;