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

[FEATURE] Simplify early returns #15

Closed
michaelboyles opened this issue Nov 6, 2021 · 0 comments
Closed

[FEATURE] Simplify early returns #15

michaelboyles opened this issue Nov 6, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@michaelboyles
Copy link
Owner

michaelboyles commented Nov 6, 2021

It might be desirable to return early within a reducer, for example

interface State {
    str: string;
}

const val = 3;
const reducer = redcr((state: State) => {
    if (val == 3) {
        state.str = 'abc';
        return;
    }
    state.str = 'def';
});

As redcr()'s signature accepts a void-returning function, it might be expected that this will work. However, the output is

const reducer = (state) => {
    if (val == 3) {
        state = { ...state, str: 'abc' };
        return; // <<< should be   return state;
    }
    state = { ...state, str: 'def' };
    return state;
};

The first branch will end up returning undefined. A workaround exists: the author can specify return state; rather than return; but maybe that's unintuitive since the function is supposed to be void-returning.

Somewhat related to #14

@michaelboyles michaelboyles added the enhancement New feature or request label Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant