Skip to content

Remove isPlainObject checking in dispatch method #1006

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

Closed
niba opened this issue Nov 4, 2015 · 3 comments
Closed

Remove isPlainObject checking in dispatch method #1006

niba opened this issue Nov 4, 2015 · 3 comments

Comments

@niba
Copy link

niba commented Nov 4, 2015

I'm writing a react/redux project in TypeScript and I would like to have a little different structure of actions and reducers.

Here is the example:

abstract class Action {
    type: string;
}

class ActionOne extends Action {
     constructor(public one: string) {
            super();
            this.type = "TypeOne";
        }
}

class ActionTwo extends Action {
     constructor(public two: number) {
            super();
            this.type = "TypeTwo";
        }
}

and my reducer method

function reducer(state, action: Action) {
    if(action instanceof ActionOne) {
        action.one; // works
                action.two; // error
    } else if (action instanceof ActionTwo) {
        action.one; // error
                action.two; // works
    } else {
        return state;
    }
}

Thank to these changes and to type guard feature I have intellisense and type checking inside of each if/else body.
The only problem with this solution is that dispatch method checks if the action object is the plain object which in my case is not true.

@gaearon
Copy link
Contributor

gaearon commented Nov 4, 2015

Please see the discussion in #992.

@gaearon gaearon closed this as completed Nov 4, 2015
@gaearon
Copy link
Contributor

gaearon commented Nov 17, 2015

Nice workaround for TypeScript 1.6: #992 (comment)

@niba
Copy link
Author

niba commented Nov 18, 2015

@gaearon Awesome! Thanks!

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

2 participants