React Navigation + TypeScript Error: Type 'EventStackParams' does not satisfy the constraint 'Record<string, object | undefined>'
React Navigation + TypeScript Error: Type 'EventStackParams' does not satisfy the constraint 'Record<string, object | undefined>'
我正在将 TypeScript 添加到我的 React Native/React Navigation 5 Navigator,但在将 EventStackParams
添加到 createStackNavigator()
.
时遇到问题
我查看了 React Native 5 Docs、Whosebug 和 GitHub,但没有成功。我究竟做错了什么?下面是我的错误和代码
错误:
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,
},
};
更新:
已通过将 interface
更改为 type
来解决此问题。
// TypeScript Type: Event Stack Params
export type EventStackParams = {
Events: undefined,
EventDetails: {
item: Event,
},
};
我正在将 TypeScript 添加到我的 React Native/React Navigation 5 Navigator,但在将 EventStackParams
添加到 createStackNavigator()
.
我查看了 React Native 5 Docs、Whosebug 和 GitHub,但没有成功。我究竟做错了什么?下面是我的错误和代码
错误:
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,
},
};
更新:
已通过将 interface
更改为 type
来解决此问题。
// TypeScript Type: Event Stack Params
export type EventStackParams = {
Events: undefined,
EventDetails: {
item: Event,
},
};