Skip to content

[Feature Request] Types: Remove nullish check if initialData is given in useRequest #165

Open
@Tanimodori

Description

@Tanimodori

需求描述 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

  1. Remove the | undefined part in State
export type State<R, P> = {
    loading: Ref<boolean>;
    data: Ref<R>; // <- Remove undefined
    error: Ref<Error | undefined>;
    params: Ref<P>;
};
  1. 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;
};
  1. Modify the QueryState interface and useRequest 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.

其他信息 Other information

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions