Skip to content

Commit

Permalink
fix: remove NonNullable to fix recursive infer in PathConfigMap
Browse files Browse the repository at this point in the history
  • Loading branch information
johankasperi committed Apr 26, 2024
1 parent d0abdee commit e1249ea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
42 changes: 37 additions & 5 deletions example/__typechecks__/common.check.tsx
Expand Up @@ -8,11 +8,12 @@ import type {
DrawerNavigationOptions,
DrawerScreenProps,
} from '@react-navigation/drawer';
import type {
CompositeScreenProps,
NavigationAction,
NavigationHelpers,
NavigatorScreenParams,
import {
type CompositeScreenProps,
type LinkingOptions,
type NavigationAction,
type NavigationHelpers,
type NavigatorScreenParams,
} from '@react-navigation/native';
import {
createStackNavigator,
Expand Down Expand Up @@ -498,3 +499,34 @@ const FourthStack = createStackNavigator<FourthParamList, 'MyID'>();
expectTypeOf(FourthStack.Navigator).parameter(0).toMatchTypeOf<{
id: 'MyID';
}>();

/**
* Infer correct LinkingOptions
*/
export const linkingOptions: LinkingOptions<RootStackParamList> = {
prefixes: ['reactnavigation://'],
config: {
screens: {
Home: {
screens: {
Feed: {
screens: {
Popular: 'popular',
Latest: 'latest',
// @ts-expect-error "NonExistingRoute" does not exist in the FeedTabParamList
NonExistingRoute: 'non-existing',
},
},
Account: 'account',
},
},
PostDetails: 'post/:id',
Login: {
path: 'login',
// @ts-expect-error The Login route is not a nested navigator and should not have a screens object
screens: {},
},
NotFound: '*',
},
},
};
2 changes: 1 addition & 1 deletion example/src/Screens/NotFound.tsx
Expand Up @@ -14,7 +14,7 @@ export const NotFound = ({
<Text style={styles.title}>404 Not Found ({route.path})</Text>
<Button
variant="filled"
onPress={() => navigation.navigate('Home')}
onPress={() => navigation.navigate('Home', { screen: 'Examples' })}
style={styles.button}
>
Go to home
Expand Down
2 changes: 1 addition & 1 deletion example/src/screens.tsx
Expand Up @@ -76,7 +76,7 @@ export type RootDrawerParamList = {
};

export type RootStackParamList = ExampleScreensParamList & {
Home: NavigatorScreenParams<RootDrawerParamList> | undefined;
Home: NavigatorScreenParams<RootDrawerParamList>;
NotFound: undefined;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/types.tsx
Expand Up @@ -1005,9 +1005,9 @@ export type PathConfig<ParamList extends {}> = {
};

export type PathConfigMap<ParamList extends {}> = {
[RouteName in keyof ParamList]?: NonNullable<
ParamList[RouteName]
> extends NavigatorScreenParams<infer T extends {}>
[RouteName in keyof ParamList]?: ParamList[RouteName] extends NavigatorScreenParams<
infer T
>
? string | PathConfig<T>
: string | Omit<PathConfig<{}>, 'screens' | 'initialRouteName'>;
};

0 comments on commit e1249ea

Please sign in to comment.