React-Navigation和TypeScript。类型中缺少属性'getScreen'

时间:2022-05-18 11:45:30

Using TypeScript and react-navigation is get the following TypeScript issue:

使用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; }'.

Here is the code in my navigator:

这是我的导航器中的代码:

const MainNavigator: NavigationContainer = StackNavigator({
    Principles: {
      screen: props => <WebViewModal source={{ uri: MY_URI }} {...props} />,
      path: 'principles',
    },
})

And that is the code of the WebViewModal:

这就是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;

How can I fix this typescript issue?

我该如何解决这个打字稿问题?

1 个解决方案

#1


1  

Looking at the index.d.ts of react-navigation, I see that you can do something like:

查看react-navigation的index.d.ts,我看到你可以做类似的事情:

Principles: {
  getScreen: () => props => <WebViewModal source={{ uri: MY_URI }} {...props} />,
  path: 'principles',
},

However, I was also able to get rid of this error when I saw it without changing the code, but simply deleted node_modules and yarn.lock and ran yarn install again.

但是,当我在不更改代码的情况下看到它时,我也能够摆脱这个错误,但只是删除了node_modules和yarn.lock并再次运行yarn install。

#1


1  

Looking at the index.d.ts of react-navigation, I see that you can do something like:

查看react-navigation的index.d.ts,我看到你可以做类似的事情:

Principles: {
  getScreen: () => props => <WebViewModal source={{ uri: MY_URI }} {...props} />,
  path: 'principles',
},

However, I was also able to get rid of this error when I saw it without changing the code, but simply deleted node_modules and yarn.lock and ran yarn install again.

但是,当我在不更改代码的情况下看到它时,我也能够摆脱这个错误,但只是删除了node_modules和yarn.lock并再次运行yarn install。