piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

CreateAsyncActions function name is an empty string #104

Closed Sean-Brown-Digi closed 5 years ago

Sean-Brown-Digi commented 5 years ago

I created some async actions:

export default createAsyncAction(
    'LOGIN',
    'LOGIN_SUCCESS',
    'LOGIN_FAILURE'
)<string, string, Error>();

In the reducer I'm trying to switch based on the action type , however I can't seem to get the switch statement correct, maybe you could help?

import actions from './actions';
export default (state = '', action: any) => {
    switch (action.type) {
        case getType(actions.request.name):
            return state;
        case getType(actions.success.name):
            return state;
        case getType(actions.failure.name):
            return state;
        default:
            console.log(`unknown action`, action);
            return state;
    }
}

I tried the switch statement both with and without the .name property and it always goes to the default case. When debugging I noticed that the .name property is always an empty string, which is surprising to me because that property says Returns the name of the function. Function names are read-only and can not be changed..

How can I get the switch statement to work using the object produced by CreateAsyncActions()?

Sean-Brown-Digi commented 5 years ago

Figured it out right after posting this... just got to call toString() on the action type:

import actions from './actions';
export default (state = '', action: any) => {
    switch (action.type.toString()) {
        case getType(actions.request):
            return state;
        case getType(actions.success):
            return state;
        case getType(actions.failure):
            return state;
        default:
            console.log(`unknown action`, action);
            return state;
    }
}