[在使用react-navigation 5时出现一些TypeScript错误。关于我如何输入内容/构建应用程序的方式,我确定这是我做错的事情,但是我不确定是什么。
我从文档开始遵循这些指南:
https://reactnavigation.org/docs/en/tab-based-navigation.html#a-stack-navigator-for-each-tabhttps://reactnavigation.org/docs/en/typescript.html
我想拥有这样的应用程序结构:
一切运行正常,但是尝试从一个堆栈中的页面导航到另一个堆栈中的页面时遇到一些错误,例如,从新闻详细信息页面导航至设置页面] >。
这是我目前拥有的代码。
import * as React from 'react'; import { Button, Text, View } from 'react-native'; import { createStackNavigator, StackNavigationProp } from '@react-navigation/stack'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { RouteProp } from '@react-navigation/core'; enum AppRoute { // Home Stack HOME = 'HOME', NEWS_DETAILS = 'NEWS_DETAILS', // Settings Stack SETTINGS = 'SETTINGS', } type HomeStackParamList = { [AppRoute.HOME]: undefined; [AppRoute.NEWS_DETAILS]: { id: number; }; } type SettingsStackParamList = { [AppRoute.SETTINGS]: undefined; } type HomeProps = { navigation: StackNavigationProp< HomeStackParamList, AppRoute.HOME >; route: RouteProp<HomeStackParamList, AppRoute.HOME>; } type NewsDetailsProps = { navigation: StackNavigationProp< HomeStackParamList, AppRoute.NEWS_DETAILS >; route: RouteProp<HomeStackParamList, AppRoute.NEWS_DETAILS>; } type SettingsProps = { navigation: StackNavigationProp< SettingsStackParamList, AppRoute.SETTINGS >; route: RouteProp<SettingsStackParamList, AppRoute.SETTINGS>; } // ---------------------------------------------------------------------------- function HomeScreen(props: HomeProps) { const { navigation } = props; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate(AppRoute.NEWS_DETAILS, { id: 1 })} /> <Button title="Go to Settings" onPress={() => navigation.navigate(AppRoute.SETTINGS)} /> </View> ); } function DetailsScreen(props: NewsDetailsProps) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Details! {props.route.params.id}</Text> </View> ); } function SettingsScreen(props: SettingsProps) { const { navigation } = props; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Settings screen</Text> <Button title="Go to Home -> Details" onPress={() => navigation.navigate(AppRoute.NEWS_DETAILS, { id: 2 })} /> </View> ); } const HomeStack = createStackNavigator<HomeStackParamList>(); function HomeStackScreen() { return ( <HomeStack.Navigator> <HomeStack.Screen name={AppRoute.HOME} component={HomeScreen} /> <HomeStack.Screen name={AppRoute.NEWS_DETAILS} component={DetailsScreen} /> </HomeStack.Navigator> ); } const SettingsStack = createStackNavigator<SettingsStackParamList>(); function SettingsStackScreen() { return ( <SettingsStack.Navigator> <SettingsStack.Screen name={AppRoute.SETTINGS} component={SettingsScreen} /> </SettingsStack.Navigator> ); } const Tab = createBottomTabNavigator(); type TabNavigatorProps = React.ComponentProps<typeof Tab.Navigator>; export const AppNavigator = (props: Partial<TabNavigatorProps>): React.ReactElement => { return ( <Tab.Navigator> <Tab.Screen name="Home" component={HomeStackScreen} /> <Tab.Screen name="Settings" component={SettingsStackScreen} /> </Tab.Navigator> ); }
这是我得到的错误:
No overload matches this call. Argument of type '[AppRoute.SETTINGS]' is not assignable to parameter of type '[AppRoute.HOME | AppRoute.NEWS_DETAILS] | [AppRoute.HOME | AppRoute.NEWS_DETAILS, { id: number; }]'. Type '[AppRoute.SETTINGS]' is not assignable to type '[AppRoute.HOME | AppRoute.NEWS_DETAILS]'. Type 'AppRoute.SETTINGS' is not assignable to type 'AppRoute.HOME | AppRoute.NEWS_DETAILS'. Overload 2 of 2, '(route: { key: string; params?: { id: number; }; } | { name: AppRoute.HOME | AppRoute.NEWS_DETAILS; key?: string; params: { id: number; }; }): void', gave the following error. Argument of type 'AppRoute.SETTINGS' is not assignable to parameter of type '{ key: string; params?: { id: number; }; } | { name: AppRoute.HOME | AppRoute.NEWS_DETAILS; key?: string; params: { id: number; }; }'.ts(2769)
我想我理解为什么会这样,这是因为HomeStackParamList中没有定义设置路由。我应该为每个堆栈页面参数创建联合类型并在各处使用该联合类型,这样我就不会出现此错误,或者有没有更好的方法来构造此类型?
我以为我可以像这样创建所有参数列表的联合类型,但是我不确定这是否是正确的方法?像这样的东西:
type HomeStackParamList = { [AppRoute.HOME]: undefined; [AppRoute.HOME_DETAILS]: { id: number; }; } type SettingsStackParamList = { [AppRoute.SETTINGS]: undefined; } type AppStackParamList = HomeStackParamList & SettingsStackParamList; type HomeProps = { navigation: StackNavigationProp< AppStackParamList, AppRoute.HOME >; route: RouteProp<AppStackParamList, AppRoute.HOME>; } type DetailsProps = { navigation: StackNavigationProp< AppStackParamList, AppRoute.HOME_DETAILS >; route: RouteProp<AppStackParamList, AppRoute.HOME_DETAILS>; } type SettingsProps = { navigation: StackNavigationProp< AppStackParamList, AppRoute.SETTINGS >; route: RouteProp<AppStackParamList, AppRoute.SETTINGS>; }
任何向我指出正确方向的帮助建议,谢谢
我在使用react-navigation 5时遇到一些TypeScript错误。关于我如何输入内容/构建应用程序的方式,我确定这是我做错的事情,但不确定。我是...
是的,对于此用例,参数列表的并集很好。