我正在使用GraphQL代码生成器来创建架构实体的打字稿类型。当我使用如下所示的useQuery时,没有问题;
const { loading, error, data } = useQuery<
FavoritesQuery,
FavoritesQueryVariables
>({
fetchPolicy: 'no-cache',
});
但是,当我尝试如下所示的惰性查询时,打字稿编译器会受苦;
const [loadFavorites, { called, loading, data }] = useLazyQuery<
FavoritesQuery,
FavoritesQueryVariables
>(Queries.FAVORITES_QUERY, {
fetchPolicy: 'no-cache',
});
Intellisense在键入时会找到fetchPolicy
及其值列表。但是编译器会产生以下错误;
Argument of type '{ fetchPolicy: string; }' is not assignable to parameter of type 'LazyQueryHookOptions<{ __typename?: "MainQuery"; } & { favorites: ({ __typename?: "FavoriteGType"; } & Pick<FavoriteGType, "id" | "name" | "category" | "description" | "commandText" | "lastUpdatedDate"> & { ...; })[]; }, FavoritesQueryVariables>'.
Object literal may only specify known properties, and 'fetchPolicy' does not exist in type 'LazyQueryHookOptions<{ __typename?: "MainQuery"; } & { favorites: ({ __typename?: "FavoriteGType"; } & Pick<FavoriteGType, "id" | "name" | "category" | "description" | "commandText" | "lastUpdatedDate"> & { ...; })[]; }, FavoritesQueryVariables>'. TS2345
89 | FavoritesQueryVariables
90 | >(Queries.FAVORITES_QUERY, {
> 91 | fetchPolicy: 'no-cache',
| ^
92 | });
我应该如何在useLazyQuery中使用fetchPolicy?
感谢与问候
答案 0 :(得分:2)
React Apollo 3使用Typescript的Omit
功能,该功能已在Typescript 3.5中添加。我觉得您使用的是较旧版本的Typescript;如果更新到TS> = 3.5,则该问题将消失。