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

Make actions compatible with new Redux Toolkit guidance on TypeScript usage #230

Open
AndrewCraswell opened this issue Apr 14, 2020 · 13 comments
Assignees

Comments

@AndrewCraswell
Copy link

AndrewCraswell commented Apr 14, 2020

Issuehunt badges

Is your feature request related to a real problem or use-case?

The Redux team has released the Redux Toolkit library for its v1 release in Oct, 2019. This simplifies implementation scenarios with Redux, and they're encouraging users to adopt it for many scenarios. However, their Action type is not fully compatible with typesafe-actions.

The Redux documentation describes how actions should be able to be used with createReducer:

{
  const increment = createAction<number, 'increment'>('increment')
  const decrement = createAction<number, 'decrement'>('decrement')
  createReducer(0, {
    [increment.type]: (state, action) => {
      // action is any here
    },
    [decrement.type]: (state, action: PayloadAction<string>) => {
      // even though action should actually be PayloadAction<number>, TypeScript can't detect that and won't give a warning here.
    }
  })
}

They achieve this by having all their Actions extend the BaseActionCreator type which adds a type property and a match function for easier integration with Redux tooling.

interface BaseActionCreator<P, T extends string, M = never, E = never> {
  type: T
  match(action: Action<unknown>): action is PayloadAction<P, T, M, E>
}

[see code]

I'm building a library that uses both the Redux Toolkit, and typesafe-actions, but I'm concerned that none of the actions I create with typesafe-actions will be usable in applications using the official Redux Toolkit.

Describe a solution including usage in code example

Seems like the actions from typesafe-actions could extend the same properties the Redux Toolkit does, and we could benefit with interoperability and the better type safety as described by the Redux docs.

Who does this impact? Who is this for?

Users of the Redux Toolkit, or building libraries which should interop with official Redux tooling.

This could also positively impact the following issue: #214


IssueHunt Summary

Backers (Total: $50.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@issuehunt-oss
Copy link

issuehunt-oss bot commented Apr 14, 2020

@andrewcraswell has funded $200.00 to this issue.


@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label Apr 14, 2020
@piotrwitek piotrwitek self-assigned this Apr 14, 2020
@piotrwitek
Copy link
Owner

Let me experiment with this idea and we'll look into what is possible.

@issuehunt-oss
Copy link

issuehunt-oss bot commented Apr 16, 2020

@andrewcraswell has cancelled funding for this issue.(Cancelled amount: $200.00) See it on IssueHunt

@issuehunt-oss issuehunt-oss bot removed the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label Apr 16, 2020
@issuehunt-oss
Copy link

issuehunt-oss bot commented Apr 16, 2020

@andrewcraswell has funded $50.00 to this issue.


@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label Apr 16, 2020
@AndrewCraswell
Copy link
Author

I still think this is a useful feature, but I found a workaround by reusing some of the types from the Redux Toolkit directly. Going to reduce the bounty on this, and convert some of it to a direct donation.

@avin-kavish
Copy link

Why do we need feature parity with redux toolkit? This is the original library and I personally like this interface better.

@AndrewCraswell
Copy link
Author

AndrewCraswell commented Apr 21, 2020

It's definitely not feature parity, it's making sure things that are built with the official Toolkit are not broken in this library. Essentially, why I had to migrate away from typesafe-actions. I can't use it to build a library which won't work with the official Redux recommended way of doing things.

Is there something specific you like better about not including the type and match on the action creator?

@avin-kavish
Copy link

avin-kavish commented Apr 24, 2020

I like createReducer but I take it back redux-toolkit is probably going to make this library redundant. I don't think it is because it is "official" but because the tools are really good. It's like the next generation of this.

@mighty-phoenix
Copy link

Hey!
Is this issue still up for grabs(Issuehunt bouny)?
Thanks! @piotrwitek @kononenko-a-m

@AndrewCraswell
Copy link
Author

I had forgotten about this, but it is still funded. I'm not sure how relevant it is anymore, given that Redux Toolkit has had multiple major releases since. But if an implementation for this is submitted, I'll honor it.

@markerikson
Copy link

@AndrewCraswell for the record, we've had multiple minor releases of RTK, but no major releases :) we're actually about to publish RTK 2.0 hopefully in the next few weeks, though!

@EskiMojo14
Copy link

In case anyone is still interested, I've made the necessary code changes in a branch here. I just don't understand enough about dts-jest to get a PR in 😅 Feel free to use!

@piotrwitek
Copy link
Owner

@EskiMojo14 thanks I'll open PR, if tests are passing I'll consider merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants