使用 TypeScript 和 react-navigation 会得到以下 TypeScript 问题:
Property 'Principles' is incompatible with index signature.
Type '{ screen: (props: any) => Element; path: string; }' is not assignable to type 'NavigationRouteConfig'.
Type '{ screen: (props: any) => Element; path: string; }' is not assignable to type '{ navigationOptions?: any; path?: string; } & { getScreen: () => NavigationComponent; }'.
Type '{ screen: (props: any) => Element; path: string; }' is not assignable to type '{ getScreen: () => NavigationComponent; }'.
Property 'getScreen' is missing in type '{ screen: (props: any) => Element; path: string; }'.
这是我的导航器中的代码:
const MainNavigator: NavigationContainer = StackNavigator({
Principles: {
screen: props => <WebViewModal source={{ uri: MY_URI }} {...props} />,
path: 'principles',
},
})
这就是以下代码WebViewModal
:
export interface IWebViewModalProps extends WebViewProperties, NavigationScreenProps {}
class WebViewModal extends Component<IWebViewModalProps> {
closeModal = () => {
this.props.navigation.goBack();
};
render() {
const {...props } = this.props;
return (
<View style={styles.container}>
<TopNavigationBar left={<Icon name="close" onPress={this.closeModal} />} />
<WebView {...props} />
</View>
);
}
}
export default WebViewModal;
我该如何解决这个打字稿问题?
查看 react-navigation 的 index.d.ts ,我看到您可以执行以下操作:
Principles: {
getScreen: () => props => <WebViewModal source={{ uri: MY_URI }} {...props} />,
path: 'principles',
},
但是,当我看到这个错误时,我也能够摆脱这个错误而无需更改代码,只需删除 node_modules 和 yarn.lock 并再次运行yarn install。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句