我开始在我的本机项目(打字稿新手)中使用打字稿,并在使用打字稿的功能组件中遇到了NavigationOptions的问题,这要归功于这篇文章here,但是当我尝试通过redux props告诉我的时候
Property 'authType' does not exist on type 'PropsWithChildren<NavigationStackScreenProps<Params, Props>>
这是我的代码
interface Props {
authType: string;
}
interface Params {
routeName: string;
}
const Customer: NavigationStackScreenComponent<Params, Props> = ({
navigation,
authType,
}) => {
.... Component
}
Customer.navigationOptions = ({ navigation }) => ({
title: navigation.getParam('routeName'),
});
[我认为这意味着尽管我通过了道具,但我没有通过它。
我使用NavigationStackScreenComponent以便可以使用
Customer.navigationOptions etc
否则它会告诉我
ts] Property 'navigationOptions' does not exist
我真的是开始喜欢打字稿,知道如何使用authType道具而又不会对我大吼大叫吗?
您需要执行类似操作
interface Props {
authType: string;
}
type CustomerType = React.ComponentType<Props & NavigationStackScreenProps> & { navigationOptions: NavigationStackOptions | ((props: NavigationStackScreenProps) => NavigationStackOptions) }
const Customer: CustomerType = ({
navigation,
authType,
}) => {
.... Component
}
我最终这样做了
type Params = {
routeName: string;
};
type Props = {
authType?: string;
navigation: NavigationStackProp;
};
const Customer: NavigationStackScreenComponent<Params> = ({
authType,
navigation,
}: Props) => {
我刚刚将传递的道具设为可选。