使用 typescript 进行原生反应 - 类型参数 '[never, { email: string;密码:字符串; }]' 不可分配给“never”类型的参数

问题描述 投票:0回答:1

我正在与 expo 合作,并尝试使用导航将道具推送到其他屏幕,代码编译并推送道具,但在将道具作为第二个参数传递给 navigation.navigate() 函数时出现此错误:

Argument of type '[never, { email: string; password: string; }]' is not assignable to parameter of type 'never'.

在尝试导航到屏幕而不传递道具时,我也会收到此错误:

Argument of type 'string' is not assignable to parameter of type '{ key: string; params?: undefined; merge?: boolean | undefined; } | { name: "SignUp"; key?: string | undefined; params: undefined; merge?: boolean | undefined; } | { key: string; params?: { email: string; password: string; } | undefined; merge?: boolean | undefined; } | ... 10 more ... | { ...; }'.ts(2345)

当我添加“从未如此”时,它就消失了。

这是我在 SignUp.tsx 屏幕中调用该函数的地方:

import { RootStackParamList } from "../../App";
import { NativeStackScreenProps } from "@react-navigation/native-stack";
type propsType = NativeStackScreenProps<RootStackParamList, 'SignUp'>;

const SignUp = (props: propsType) => {
    const {navigation} = props;

.......

            return(
               ...
                <MainButton 
                canGoBack 
                style={tw`h-20 justify-center mt-3 mx-auto`} 
                text={"Sign up"} 
                targetPage="Characteristics" 
                onPress={() => navigation.navigate("Characteristics" )} 
                navigationData={{ email: formInput.email, password: formInput.password }} />)

这是 App.tsx,其中我有 RootStackParamList 类型以及堆栈导航器:

export type RootStackParamList = {
    Login: undefined;
    SignUp: undefined;
    ProfileSetup: undefined;
    ProfileSetupDetails: undefined;
    IdealMatch: undefined;
    Characteristics: {
        email: string,
        password: string;
    };
    Profile: undefined;
};

const Stack = createNativeStackNavigator<RootStackParamList>();

export type Props = NativeStackScreenProps<RootStackParamList, 'Characteristics'>;



const App: React.FC<RootStackParamList> = () => {
    return (
      <NavigationContainer>
          <Stack.Navigator>
              <Stack.Screen name="Login" component={Login}
                             options={{ headerShown: false }}/>
              <Stack.Screen name="SignUp" component={SignUp}
                             options={{ headerShown: false }}/>
              <Stack.Screen name="ProfileSetup" component={ProfileSetup}
                             options={{ headerShown: false }}/>
              <Stack.Screen name="ProfileSetupDetails" component={ProfileSetupDetails}
                             options={{ headerShown: false }}/>
              <Stack.Screen name="IdealMatch" component={IdealMatch}
                             options={{ headerShown: false }}/>
              <Stack.Screen name="Characteristics" component={Characteristics}
                             options={{ headerShown: false }}
                             />
              <Stack.Screen name="Profile" component={Profile}
                             options={{ headerShown: false }}/>
          </Stack.Navigator>
      </NavigationContainer>
    );
}

我需要建议,相对较新的反应本机和反应导航。

使用路径名和我推送的数据创建一个 route const 并将其传递给导航似乎可以修复它,但我不知道是否可以这样保留它:

const route = ["Characteristics", { email: formInput.email, password: formInput: password}];
navigation.navigate(...(route as never));
typescript react-native expo react-navigation
1个回答
0
投票

通过创建导航常量并从我的注册屏幕中删除 props: propsType arg 来修复它:

const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();

我仍然想知道是否必须在堆栈的每个屏幕上创建一个新的导航常量?

© www.soinside.com 2019 - 2024. All rights reserved.