Skip to content

Commit

Permalink
fix: fix type of setOptions and mark data passed to callbacks as Read…
Browse files Browse the repository at this point in the history
…only
  • Loading branch information
satya164 committed Feb 17, 2023
1 parent 193cc21 commit 6655c66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
23 changes: 4 additions & 19 deletions example/types.check.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const PostDetailsScreen = ({

expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<StackNavigationOptions>();
.toMatchTypeOf<Partial<StackNavigationOptions>>();

expectTypeOf(navigation.addListener)
.parameter(0)
Expand Down Expand Up @@ -126,10 +126,7 @@ export const FeedScreen = ({

expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<StackNavigationOptions>();
expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<DrawerNavigationOptions>();
.toMatchTypeOf<Partial<DrawerNavigationOptions>>();

expectTypeOf(navigation.addListener)
.parameter(0)
Expand Down Expand Up @@ -160,13 +157,7 @@ export const PopularScreen = ({

expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<StackNavigationOptions>();
expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<DrawerNavigationOptions>();
expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<BottomTabNavigationOptions>();
.toMatchTypeOf<Partial<BottomTabNavigationOptions>>();

expectTypeOf(navigation.addListener)
.parameter(0)
Expand Down Expand Up @@ -201,13 +192,7 @@ export const LatestScreen = ({

expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<StackNavigationOptions>();
expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<DrawerNavigationOptions>();
expectTypeOf(navigation.setOptions)
.parameter(0)
.toMatchTypeOf<BottomTabNavigationOptions>();
.toMatchTypeOf<Partial<BottomTabNavigationOptions>>();

expectTypeOf(navigation.setParams).parameter(0).toEqualTypeOf<undefined>();

Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type NavigationHelpersCommon<
* @param action Action object or update function.
*/
dispatch(
action: NavigationAction | ((state: State) => NavigationAction)
action: NavigationAction | ((state: Readonly<State>) => NavigationAction)
): void;

/**
Expand Down Expand Up @@ -323,15 +323,15 @@ export type NavigationContainerProps = {
/**
* Callback which is called with the latest navigation state when it changes.
*/
onStateChange?: (state: NavigationState | undefined) => void;
onStateChange?: (state: Readonly<NavigationState> | undefined) => void;
/**
* Callback which is called after the navigation tree mounts.
*/
onReady?: () => void;
/**
* Callback which is called when an action is not handled.
*/
onUnhandledAction?: (action: NavigationAction) => void;
onUnhandledAction?: (action: Readonly<NavigationAction>) => void;
/**
* Whether child navigator should handle a navigation action.
* The child navigator needs to be mounted before it can handle the action.
Expand Down Expand Up @@ -381,7 +381,7 @@ export type NavigationProp<
* Update the options for the route.
* The options object will be shallow merged with default options object.
*
* @param options Options object for the route.
* @param update Options object or a callback which takes the options from navigator config and returns a new options object.
*/
setOptions(options: Partial<ScreenOptions>): void;
} & EventConsumer<EventMap & EventMapCore<State>> &
Expand Down Expand Up @@ -418,11 +418,9 @@ export type CompositeNavigationProp<
*/
A extends NavigationProp<any, any, any, infer S> ? S : NavigationState,
/**
* Screen options from both navigation objects needs to be combined
* This allows typechecking `setOptions`
* Screen options should refer to the options specified in the first type
*/
(A extends NavigationProp<any, any, any, any, infer O> ? O : {}) &
(B extends NavigationProp<any, any, any, any, infer P> ? P : {}),
A extends NavigationProp<any, any, any, any, infer O> ? O : {},
/**
* Event consumer config should refer to the config specified in the first type
* This allows typechecking `addListener`/`removeListener`
Expand Down Expand Up @@ -573,7 +571,11 @@ export type RouteConfig<
* For a given screen name, there will always be only one screen corresponding to an ID.
* If `undefined` is returned, it acts same as no `getId` being specified.
*/
getId?: ({ params }: { params: ParamList[RouteName] }) => string | undefined;
getId?: ({
params,
}: {
params: Readonly<ParamList[RouteName]>;
}) => string | undefined;

/**
* Initial params object for the route.
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/useNavigationCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ export default function useNavigationCache<

return rest.getParent(id);
},
setOptions: (options: object) =>
setOptions: (options: object) => {
setOptions((o) => ({
...o,
[route.key]: { ...o[route.key], ...options },
})),
}));
},
isFocused: () => {
const state = getState();

Expand Down

0 comments on commit 6655c66

Please sign in to comment.