React Navigation + TypeScript Error: Type 'EventStackParams' does not satisfy the constraint 'Record<string, object | undefined>'

倾然丶 夕夏残阳落幕 提交于 2021-01-05 08:53:19

问题


I'm adding TypeScript to my React Native/React Navigation 5 Navigator, but having an issue with Adding EventStackParams to the createStackNavigator().

I've looked through the React Native 5 Docs, StackOverflow, and GitHub, but no luck. What am I doing wrong? Below is my error and the code

Error:

Type 'EventStackParams' does not satisfy the constraint 'Record<string, object | undefined>'.

Index signature is missing in type 'EventStackParams'.ts(2344)

Navigation.tsx:

// Imports: TypeScript Types
import { EventStackParams } from '../types/types';

// React Navigation: Stack Navigators
const RootStack = createStackNavigator();
const EventsStack = createStackNavigator<EventStackParams>();

// Events Navigator
const EventsNavigator = () => (
  <EventsStack.Navigator initialRouteName="Events">
    <EventsStack.Screen
      name="Events"
      component={Events}
      options={{
        title: '',
        headerShown: false,
      }}
    />

    <EventsStack.Screen
      name="EventDetails"
      component={EventDetails}
      options={{
        title: '',
        headerShown: true,
        headerStyle: {
          elevation: 0,
          shadowOpacity: 0,
          borderBottomWidth: 0,
        },
      }}
    />
  </EventsStack.Navigator>
);

EventDetails.tsx

// Imports: Dependencies
import { RouteProp } from '@react-navigation/native';

// Imports: TypeScript Types
import { ReduxState, EventStackParams } from '../../types/types';

// React Hooks: React Navigation
const route = useRoute<RouteProp<EventStackParams, 'EventDetails'>>();

Types.tsx:

// TypeScript Type: Event
export interface Event {
  eventTitle: string,
  eventDescription: string | null,
  eventStreet: string,
  eventLocation: string,
  eventLatitude: number,
  eventLongitude: number,
  eventDateStart: number,
  eventDateEnd: number,
  eventTimeStart: string,
  eventTimeEnd: string,
};

// TypeScript Type: Event Stack Params
export interface EventStackParams {
  Events: undefined,
  EventDetails: {
    item: Event,
  },
};


回答1:


Update:

This was fixed by changing the interface to type.

// TypeScript Type: Event Stack Params
export type EventStackParams = {
  Events: undefined,
  EventDetails: {
    item: Event,
  },
};


来源:https://stackoverflow.com/questions/64903342/react-navigation-typescript-error-type-eventstackparams-does-not-satisfy-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!