REACT查询 - 将使用USEQUERY与类型安全的多个自定义钩中的属性结合在一起
我有一个任意数量的自定义钩子,可以返回usequery的查询结果。我要做的是将这些钩子的返回值与此结构结合到一个对象中:
{
data,
isLoading,
isFetching,
isSuccess,
}
数据属性将是通过在所有结果上组合数据属性创建的数组。 这是我当前的代码:
export function isAnyProperty(
property: keyof QueryObserverBaseResult<unknown, unknown>,
...queries: UseQueryResult<unknown, unknown>[]
) {
return queries.some((query) => query[property]);
}
export function combineQueryResults<TData extends unknown[], TQueries extends UseQueryResult<TData>[]>(
...queries: TQueries
) {
const data = queries.reduce<TData[]>((acc, query) => {
if (query.data === undefined) {
return acc;
}
return [...acc, query.data];
}, []);
return {
data,
isLoading: isAnyProperty('isLoading', ...queries),
isFetching: isAnyProperty('isFetching', ...queries),
isSuccess: isAnyProperty('isSuccess', ...queries),
};
}
当我使用REST参数时,我无法获得Typescript来推断tdata
的值,我不确定为什么。要注意的一件事是,当data
是usequeryResult
时,我确实设法使Typescript推断正确的类型/代码>。
为了参考,我目前从数据
是未知[] [] []
中返回的内容。我期望回来的情况当然是一个带有数据
值的元组,useQueryResult
。我认为也许减少函数删除所有未定义的值可能会使Typescript能够推断出正确的值 - 因此我也使用简单的映射尝试了它,这导致data :( Unknown [] | Undefined)[] []
- 因此也无效。
我还尝试了以下操作:
export function combineQueryResults<TData extends unknown[]>(...queries: UseQueryResult<TData>[]) {
const data = queries.map((query) => query.data);
return {
data,
isLoading: isAnyProperty('isLoading', ...queries),
isFetching: isAnyProperty('isFetching', ...queries),
isSuccess: isAnyProperty('isSuccess', ...queries),
};
}
但是使用此代码,Typescript只会推断第一个变量传递给combineQueryResult
的类型,并在中结果'type'typeOne []'''不可分配给type'typeTwo []'
错误。
看起来TypeScript将在与使用
useQueryResult
时推断tdata
,但不会自行:
,
vod.constructedAsset []
和group.union []
都是tdata
。
I have an arbitrary amount of custom hooks that return the query result from useQuery. What I'm trying to do is combine the return values from those hooks into one object with this structure:
{
data,
isLoading,
isFetching,
isSuccess,
}
The data property will be an array created from combining the data properties on all the results.
This is my current code:
export function isAnyProperty(
property: keyof QueryObserverBaseResult<unknown, unknown>,
...queries: UseQueryResult<unknown, unknown>[]
) {
return queries.some((query) => query[property]);
}
export function combineQueryResults<TData extends unknown[], TQueries extends UseQueryResult<TData>[]>(
...queries: TQueries
) {
const data = queries.reduce<TData[]>((acc, query) => {
if (query.data === undefined) {
return acc;
}
return [...acc, query.data];
}, []);
return {
data,
isLoading: isAnyProperty('isLoading', ...queries),
isFetching: isAnyProperty('isFetching', ...queries),
isSuccess: isAnyProperty('isSuccess', ...queries),
};
}
I can't get TypeScript to infer the values for TData
when I am using rest parameters, and I am not quite sure why. One thing to note is that I did manage to get TypeScript to infer the correct types when data
was an array of UseQueryResult
, just not one level down with TData
.
For reference, what I get back currently from data
is unknown[][]
. What I am expecting to get back is of course a tuple with the data
values from UseQueryResult
. I thought maybe the reduce function removing all undefined values might mess with TypeScript being able to infer the correct values - so I tried it with a simple map as well, which results in data: (unknown[] | undefined)[]
- so that didn't work either.
I also tried this:
export function combineQueryResults<TData extends unknown[]>(...queries: UseQueryResult<TData>[]) {
const data = queries.map((query) => query.data);
return {
data,
isLoading: isAnyProperty('isLoading', ...queries),
isFetching: isAnyProperty('isFetching', ...queries),
isSuccess: isAnyProperty('isSuccess', ...queries),
};
}
But with this code, typescript will only infer the type for the first variable passed to combineQueryResult
and results in a Type 'TypeOne[]' is not assignable to type 'TypeTwo[]'
error.
Looks like typescript will infer the type TData
when used with UseQueryResult
, but not on its own:
unknown[]
, Vod.ConstructedAsset[]
, and Group.Union[]
are all TData
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了此功能以结合我的查询:
希望这对您有所帮助。
I created this function to combine my queries:
I hope this helps you.