reactjs - 导出 react-query 封装
我有 2 个包,并且将 react-query 作为依赖项:
/UI
/node_modules
/common
/common
/node_modules
/react-query
在我的通用包中,我有一些 react-query 包装器,它们封装了一些特定的逻辑。
包装器的示意例子:
export const useQuery = <
TData = unknown,
TError = unknown
>(
queryKey: QueryKey,
queryFunction: QueryFunction<AxiosResponse<TData, any>>,
options?: UseQueryOptions<TData, TError>
): UseQueryResult<TData, TError> => {
return useQueryOriginal<TData, TError, TData>(
queryKey,
(context) => queryFunction(context).then(r => r.data),
options);
};
现在,在我的 UI 包中,我想创建一个具体的钩子:
import { useQuery } from 'common';
const useMyConrecteQuery = () => useQuery(['key'], () => apiCall());
对此我收到错误:
The inferred type of 'useMyConcreteQuery' cannot be named without a reference to 'common/node_modules/react-query'. This is likely not portable. A type annotation is necessary.
我尝试过:
- 在公共包中重新声明所有使用的类型,但那没有帮助
- 在 tsconfig 中添加 `declaration: true`