Skip to content

[enhancement]: Type Safety in RTK Query Listener  #3951

Open
@Millu

Description

@Millu

Is there an existing issue for this?

  • I have searched the existing issues

Contact Details

No response

What should this feature add?

When we use RTK Query in the listener middleware, we'd like to get type safety on actions.

This needs some TypeScript magic, thanks to RTK maintainer on discord:

`type TypeGuard = {
(input: unknown): input is T;
};

type TypeGuardFor<T extends TypeGuard> = T extends TypeGuard
? U
: never;

// in startAppListening
matcher: (
action
): action is TypeGuardFor<
typeof imagesApi.endpoints.listImages.matchFulfilled
> =>
imagesApi.endpoints.listImages.matchFulfilled(action) &&
action.meta.arg.queryCacheKey ===
getListImagesUrl({ categories: IMAGE_CATEGORIES }),`

Alternatives

No response

Aditional Content

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions