Open
Description
需求描述 Feature Description
If initialData
is given, the result type should not have the undefined
part.
The ref
function in vue
is a good example:
export declare function ref<T>(value: T): Ref<UnwrapRef<T>>;
export declare function ref<T = any>(): Ref<T | undefined>;
建议的解决方案 Proposed Solution
- Remove the
| undefined
part inState
export type State<R, P> = {
loading: Ref<boolean>;
data: Ref<R>; // <- Remove undefined
error: Ref<Error | undefined>;
params: Ref<P>;
};
- Add a type param for
Option
// Add typeParam D
export type Options<R, P extends unknown[], D = undefined> = BaseOptions & {
defaultParams?: P;
ready?: Ref<boolean>;
initialData?: D; // Changed to D
refreshDeps?: WatchSource<any>[];
cacheKey?: string | ((params?: P) => string);
refreshDepsAction?: () => void;
onSuccess?: (data: R, params: P) => void;
onError?: (error: Error, params: P) => void;
onBefore?: (params: P) => void;
onAfter?: (params: P) => void;
};
- Modify the
QueryState
interface anduseRequest
signature.
// Add typeParam D
export interface QueryState<R, P extends unknown[], D = undefined> extends State<R | D, P> {
run: (...arg: P) => Promise<R | null>;
cancel: () => void;
refresh: () => Promise<R | null>;
mutate: Mutate<R>;
}
// Add typeParam D
function useRequest<R, P extends unknown[] = any, D = undefined>(
service: Service<R, P>,
options?: Options<R, P, D>, // Add D
): QueryResult<R, P, D>; // Add D
Same modification is needed for useLoadMore
, usePagination
.