如何解决ApolloClient客户的类型
我有一个函数,我将客户端作为参数传递,并从我的应用程序等中删除令牌:
const logOut = async (client: ApolloClient<unknown>) => {...};
return (
<ApolloConsumer>
{(client: ApolloClient<unknown>) => (
<SafeAreaView style={styles.safeAreaViewContainer}>
<View style={styles.buttonContainer}>
<Button
onPress={() => logOut(client)}
style={styles.button}
/>
</View>
</SafeAreaView>
)}
</ApolloConsumer>
);
};
现在我为ApolloClient类型使用unknown
,它可以工作。但是我实际上应该在这里使用什么?
解决方法
如果您查看@apollo/client/react/context/ApolloConsumer.d.ts
,则可以看到客户端的类型为ApolloClient<object>
(在较早的react-apollo中,类型为ApolloClient<any>
)
import React from "react";
import { ApolloClient } from "../../core";
export interface ApolloConsumerProps {
children: (client: ApolloClient<object>) => React.ReactChild | null;
}
export declare const ApolloConsumer: React.FC<ApolloConsumerProps>;
//# sourceMappingURL=ApolloConsumer.d.ts.map
我不明确指定client
的类型。 TypeScript很聪明
来自动推断。
如果您仍然想编写更严格的类型,则ApolloClient中的泛型为TCacheShape
,这取决于您与apollo一起使用的缓存。
例如,您正在使用InMemoryCache
,通用类型为NormalizedCacheObject
export interface NormalizedCacheObject {
[dataId: string]: StoreObject | undefined;
}
export interface StoreObject {
__typename?: string;
[storeFieldName: string]: StoreValue;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。