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

Type assertion on the whole action #19

Closed
farzadmf opened this issue Mar 10, 2018 · 10 comments
Closed

Type assertion on the whole action #19

farzadmf opened this issue Mar 10, 2018 · 10 comments

Comments

@farzadmf
Copy link

farzadmf commented Mar 10, 2018

Hi,

I just found out about your library, and I think it is what I have been looking for to substantially reduce boilerplate code for actions and action creators; thank you!

Creating actions (using createAction) and switching on them (using getType) in the reducer works fine, but I was wondering if there's a way to do type checking for a method that expects a specific kind of action.

Example of my actions (I'm using lodash for values and map):

export enum ActionTypes {
  GetCameras = 'GET_CAMERAS',
  GetCamerasSuccss = 'GET_CAMERAS_SUCCESS',
}

export const cameraActions = {
  getCameras: createAction(ActionType.GetCameras, ( /*...*/ ) => ({
    type: ActionTypes.GetCameras,
    /* payload: ... */
  }),
  getCamerasSuccess: createActionTypes.GetCamerasSuccess, (/*...*/) => ({
    type: ActionTypes.GetCameras,
    /* payload: ... */
  }),
};

const returnsOfActions = _.map(_.values(actions), $call));
export type CameraAction = typeof returnsOfActions[number];

Maybe a good example would be a case where the reducer wants to call another method for a specific action type, for example, in my reducer:

export const reducer = (state = initialState, action: CameraAction) => {
  case getType(cameraActions.getCameras):
    return state; /* ... */
  case getType(cameraActions.getCamerasSuccess):
    return receiveCameras(state, action);
  default:
    return state;
};

Now, in my receiveCameras method, I want to make sure the second argument is of type cameraActions.getCamerasSuccess.

I can do something like this:

const receiveCameras = (state: CameraState, action: CameraAction) => {
  if (action.type !== getType(cameraActions.getCamerasSuccess)) {
    return;
  }
  
  // Now my action type is correct
}

I was wondering if I can specify the type when defining the parameter in the method to avoid doing that if check

Thank you

@piotrwitek
Copy link
Owner

piotrwitek commented Mar 10, 2018

Hello @farzadmf
Currently not but it's because of TS v2.7 inference engine limitation.
Fortunately you will be able to do that with the new API in v2.0 I'm currently working on.

type CameraAction = ReturnType<typeof cameraActions.getCamerasSuccess>;
const receiveCameras = (state: CameraState, action: CameraAction) => {
...

I will open issue today for discussion/feedback on the API 2.0, new features like Symbols support, but also improvements to createAction (got rid of duplicated type argument finally :)) and probably push the working implementation to the next branch for testing.

EDIT: API v2.0 discussion here: #22

@farzadmf
Copy link
Author

farzadmf commented Mar 10, 2018

Hello @piotrwitek thank you for your answer, So how I can I keep track of the new API version you mentioned and when it will available?

@piotrwitek
Copy link
Owner

2 days top I think, I'm heavily working on it

@dziamid
Copy link

dziamid commented Mar 19, 2018

Looking forward to start using ReturnType. Can I just update TS to 2.8@rc and start using ReturnType with typesafe-actions?

@duro
Copy link

duro commented Mar 22, 2018

@dziamid I have done so with success. I have a saga that looks like this:

import { sessionActions as SA } from './actions'

function* setToken(action: ReturnType<typeof SA.setToken>) { }

Where SA.setToken is an action that was built with typesafe-actions

@dziamid
Copy link

dziamid commented Mar 22, 2018

@duro, sweet, was looking for the exact same use case. Are you using the version 1 of this library?

@duro
Copy link

duro commented Mar 22, 2018 via email

@dziamid
Copy link

dziamid commented Mar 22, 2018

How exactly? Type and payload assertions are all I care about right now.

@piotrwitek
Copy link
Owner

Hi @dziamid, for example this: #21 (comment)

@piotrwitek
Copy link
Owner

piotrwitek commented Apr 4, 2018

Closing as issue seems to be unactionable.

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