Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having generic type for BaseQueryAPI #1685

Closed
JulienKode opened this issue Nov 3, 2021 · 3 comments
Closed

Having generic type for BaseQueryAPI #1685

JulienKode opened this issue Nov 3, 2021 · 3 comments

Comments

@JulienKode
Copy link
Contributor

Currently if I want to type my custom base query I'll do it with BaseQueryFn

here is the current type

export type BaseQueryFn<
  Args = any,
  Result = unknown,
  Error = unknown,
  DefinitionExtraOptions = {},
  Meta = {}
> = (
  args: Args,
  api: BaseQueryApi,
  extraOptions: DefinitionExtraOptions
) => MaybePromise<QueryReturnValue<Result, Error, Meta>>

This is really nice because it's templated for a lot of parameters:

  • Args
  • Result
  • Error
  • ExtraOptions
  • Meta

but not for BaseQueryApi which can be different for each project, let's say I implemented redux thunk with extra argument then my BaseQueryApi will have different than the current one

This is the current type of BaseQueryApi:

export interface BaseQueryApi {
  signal: AbortSignal
  dispatch: ThunkDispatch<any, any, any>
  getState: () => unknown
  extra: unknown
}

Maybe we should have something like this for people that have their own extra

export type BaseQueryFn<
  Args = any,
  Result = unknown,
  Error = unknown,
  DefinitionExtraOptions = {},
  Meta = {},
  QueryApi extends BaseQueryApi = BaseQueryApi,
> = (
  args: Args,
  api: QueryApi,
  extraOptions: DefinitionExtraOptions
) => MaybePromise<QueryReturnValue<Result, Error, Meta>>

I've opened a PR, where we can check what is the best way to implement this #1684 (The current change are not relevent but it's just an idea)

if you think it's not relevent or if you think there are other way to type it, I can close this

@phryneas
Copy link
Member

phryneas commented Nov 4, 2021

Hey, thanks for the suggestion & PR!

To be honest, unless we get the feedback a lot that this is needed, I'd prefer to keep it like it is at the moment and have people write a type assertion in BaseQuery like

const baseQuery: BaseQueryFn<string | FetchArgs,  unknown,  FetchBaseQueryError,  {},  FetchBaseQueryMeta> = async (arg, api) => {
+  const getState = api.getState as () => MyState;
+  const extra = api.extra as Something
}

I think this is not a very common use case and as long as TS Generic arguments are still positional and not named, each and every one we add makes using the whole type more weird to use, makes maintenance more difficult and locks us into certain decisions we might regret later down the path.

@JulienKode
Copy link
Contributor Author

I totally understand your point of view,
I wanted to avoid as but seems it's better to keep this in that way

We do not have a way to have the generic type, and if it's not provided still keep the current default one without complexity the current TS Implementation ?

Thanks for your feedback and help @phryneas

@thorn0
Copy link
Contributor

thorn0 commented Mar 31, 2022

@phryneas

I'd prefer to keep it like it is at the moment and have people write a type assertion in BaseQuery
const getState = api.getState as () => MyState;

This causes a circular reference issue. Please see it here https://codesandbox.io/s/elated-golick-b7kv2n?file=/src/index.tsx
Replace queryApi.getState as () => AppState with queryApi.getState as () => any and the issue is gone. Is there a way to use the AppState type inside queryFn?

@markerikson markerikson closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants