Skip to content

Conversation

@OleksiiKuchma-Wix
Copy link

@OleksiiKuchma-Wix OleksiiKuchma-Wix commented Aug 23, 2019

I'm trying to pass result of reduceReducers into createStore and it fails in typescript because of two issues:

  • createStore expects initial store type to be S | undefined instead of S | null.
  • Action aren't allowed to have fields except type.

Example code:

import reduceReducers from '../reduce-reducers';
import {createStore} from 'redux';

const reducer = (state: {}, action: {type: string, payload:any}) => {
  return {};
}

const result = reduceReducers(reducer);

const store = createStore(result);

Related issue: #33

@androsj
Copy link

androsj commented Oct 29, 2019

I was about to open an issue for this, but I see others are already having difficulties integrating this package into their TS projects. I had to copy the source code into my project and apply proper types.

Would it be too crazy to add "redux" as a dependency? I bet almost everyone who uses this is also using redux anyways.

The other more conservative approach is to copy the needed types straight from redux.
It's just 3: Reducer, Action, AnyAction

Let me know your thoughts. I'd be happy to make a pull request.

@androsj
Copy link

androsj commented Oct 29, 2019

@OleksiiKuchma-Wix I tested your provided example code using my modifications and at the very least, I can confirm the types are accepted / there are no lint errors.

@maclockard
Copy link

@OleksiiKuchma-Wix I have another version of this that may be slightly more future proof here: #38 Not sure how to best specify the dependency though

@zakharovmike
Copy link

Using the redux reducer definition as suggested in #38, I find that passing

  • more than one reducer, or
  • an initial state and one or more reducers

to reduceReducers still gives type errors.

So I tried to re-type reduceReducers to be like combineReducers in redux:

import { Action, AnyAction, Reducer } from 'redux';

export default function reduceReducers<S, A extends Action = AnyAction>(
  initialState: S | null,
  ...reducers: Reducer<S, A>[]
): Reducer<S, A>;
export default function reduceReducers<S, A extends Action = AnyAction>(
  ...reducers: Reducer<S, A>[]
): Reducer<S, A>;

And that seems to have solved the type errors.

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

Successfully merging this pull request may close these issues.

4 participants