使用TypeScript 3.9,React Native,React导航...
我收到错误:
interface StackParamList
Type 'StackParamList' does not satisfy the constraint 'Record<string, object | undefined>'.
Index signature is missing in type 'StackParamList'.ts(2344)
在以下位置:
const HomeStack = createStackNavigator<StackParamList>()
在:
const HomeStack = createStackNavigator<StackParamList>()
export interface StackParamList {
Home: undefined
Post: { post: Post }
Category: { category: Category }
Login: undefined
ForgotPassword: undefined
'My profile': undefined
'My partner': undefined
Parameters: undefined
Likes: undefined
Onboarding: undefined
}
/**
* Home "stack navigator"
* @summary this is the navigator for everything located under "home"
*/
export default function HomeStackScreen() {
return (
<>
<StatusBar backgroundColor={colors.background} barStyle="dark-content" />
<HomeStack.Navigator screenOptions={screenOptions}>
<HomeStack.Screen
name="Home"
component={HomeScreen}
options={{
headerTitle: (props) => <Logo {...props} />,
}}
/>
<HomeStack.Screen name="Login" component={LoginScreen} />
<HomeStack.Screen name="Post" component={PostScreen} options={{ headerTransparent: true, title: '' }} />
<HomeStack.Screen name="Category" component={CategoryScreen} options={({ route }) => ({ title: route.params.category.id })} />
<HomeStack.Screen name="ForgotPassword" component={ForgotPasswordScreen} />
<HomeStack.Screen name="My profile" component={UserProfileScreen} options={{ headerTransparent: true, title: '' }} />
<HomeStack.Screen name="My partner" component={UserPartnerScreen} />
<HomeStack.Screen name="Parameters" component={UserParamScreen} />
<HomeStack.Screen name="Likes" component={UserLikesScreen} />
<HomeStack.Screen name="Onboarding" component={Onboarding} options={{ headerShown: false }} />
</HomeStack.Navigator>
</>
)
}
我不明白为什么接口不能满足类型'Record‘。
我不明白“索引签名丢失”是什么意思。
你有什么想法吗?
谢谢
发布于 2020-11-19 09:02:22
我能够通过将接口更改为type来修复此问题。Here's the link to the StackOverflow post.
错误修复:
// TypeScript Type: Stack Param List
export type StackParamList = {
Home: undefined
Post: { post: Post }
Category: { category: Category }
Login: undefined
ForgotPassword: undefined
'My profile': undefined
'My partner': undefined
Parameters: undefined
Likes: undefined
Onboarding: undefined
};
发布于 2020-08-30 07:27:22
我通过添加以下内容解决了这个问题:
| Record<string, object | undefined>
在以下位置:
export type StackParamList =
| {
Home: undefined
Post: { post: Post }
Category: { category: Category }
Login: undefined
ForgotPassword: undefined
'My profile': undefined
'My partner': undefined
Parameters: undefined
Likes: undefined
Onboarding: undefined
}
| Record<string, object | undefined>
我还是不明白为什么需要它。
https://stackoverflow.com/questions/63652687
复制相似问题