Repository: refinedev/refine
File: packages/core/src/hooks/data/useList.ts (TypeScript)

Code before:
   * Resource name for API data interactions
   */
  resource?: string;

  /**
   * Tanstack Query's [useQuery](https://tanstack.com/query/v5/docs/framework/react/reference/useQuery) options
   */
  queryOptions?: UseListQueryOptions<TQueryFnData, TError, TData>;
} & BaseListProps &
  SuccessErrorNotification<
    GetListResponse<TData>,
    TError,
    Prettify<BaseListProps>
  > &
  LiveModeProps &
  UseLoadingOvertimeOptionsProps;

export type UseListReturnType<TData, TError> = {
  query: QueryObserverResult<GetListResponse<TData>, TError>;
  result: {
    data: TData[];
    total: number | undefined;
  };
} & UseLoadingOvertimeReturnType;

const EMPTY_ARRAY: readonly [] = Object.freeze([]);

/**
 * `useList` is a modified version of `react-query`'s {@link https://tanstack.com/query/v5/docs/framework/react/guides/queries `useQuery`} used for retrieving items from a `resource` with pagination, sort, and filter configurations.
 *
 * It uses the `getList` method as the query function from the `dataProvider` which is passed to `<Refine>`.
 *
 * @see {@link https://refine.dev/docs/api-reference/core/hooks/data/useList} for more details.
 *
 * @typeParam TQueryFnData - Result data returned by the query function. Extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#baserecord `BaseRecord`}
 * @typeParam TError - Custom error object that extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#httperror `HttpError`}
 * @typeParam TData - Result data returned by the `select` function. Extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#baserecord `BaseRecord`}. Defaults to `TQueryFnData`
 *
 */

export const useList = <
  TQueryFnData extends BaseRecord = BaseRecord,
  TError extends HttpError = HttpError,
  TData extends BaseRecord = TQueryFnData,
>({
  resource: resourceFromProp,
  filters,
  pagination,
  sorters,
  queryOptions,
  successNotification,

Review comment (bug):
There is a type error here. Sorry, the example code I shared in the issue was wrong. I didn’t run it, so I didn’t notice.

```
Error: packages/core prepare: src/hooks/data/useList.ts(320,7): error TS2322: Type 'readonly [] | TData[]' is not assignable to type 'TData[]'.
packages/core prepare:   The type 'readonly []' is 'readonly' and cannot be assigned to the mutable type 'TData[]'.
Error: packages/core prepare: src/hooks/data/useMany.ts(264,7): error TS2322: Type 'readonly [] | TData[]' is not assignable to type 'TData[]'.
```

You can try removing `readonly []` or adding `as` to `EMPTY_ARRAY`. Removing `Object.freeze` might also fix the type error, since it’s already outside the component It may not re-render trigger but you should double check.




Code after:
   * Resource name for API data interactions
   */
  resource?: string;

  /**
   * Tanstack Query's [useQuery](https://tanstack.com/query/v5/docs/framework/react/reference/useQuery) options
   */
  queryOptions?: UseListQueryOptions<TQueryFnData, TError, TData>;
} & BaseListProps &
  SuccessErrorNotification<
    GetListResponse<TData>,
    TError,
    Prettify<BaseListProps>
  > &
  LiveModeProps &
  UseLoadingOvertimeOptionsProps;

export type UseListReturnType<TData, TError> = {
  query: QueryObserverResult<GetListResponse<TData>, TError>;
  result: {
    data: TData[];
    total: number | undefined;
  };
} & UseLoadingOvertimeReturnType;

const EMPTY_ARRAY = Object.freeze([]) as [];

/**
 * `useList` is a modified version of `react-query`'s {@link https://tanstack.com/query/v5/docs/framework/react/guides/queries `useQuery`} used for retrieving items from a `resource` with pagination, sort, and filter configurations.
 *
 * It uses the `getList` method as the query function from the `dataProvider` which is passed to `<Refine>`.
 *
 * @see {@link https://refine.dev/docs/api-reference/core/hooks/data/useList} for more details.
 *
 * @typeParam TQueryFnData - Result data returned by the query function. Extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#baserecord `BaseRecord`}
 * @typeParam TError - Custom error object that extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#httperror `HttpError`}
 * @typeParam TData - Result data returned by the `select` function. Extends {@link https://refine.dev/docs/api-reference/core/interfaceReferences#baserecord `BaseRecord`}. Defaults to `TQueryFnData`
 *
 */

export const useList = <
  TQueryFnData extends BaseRecord = BaseRecord,
  TError extends HttpError = HttpError,
  TData extends BaseRecord = TQueryFnData,
>({
  resource: resourceFromProp,
  filters,
  pagination,
  sorters,
  queryOptions,
  successNotification,