Property type is unknown with my state store in typescript

Viewed 147

I have no idea what I am doing. My type in my scan is coming up unknown, and I think there are a number of issues besides that.

I know its something to do with the way scan works, and I replaced getState with subscribe (pretty sure that's wrong), because it was erroring and I couldn't fix it.

import Rx from 'rxjs';
import { scan, share } from 'rxjs/operators';
import { reducers } from './reducers/index';

export default function createStore(initialState, reducers) {
  const subject = new Rx.Subject();

  const store = subject.pipe(scan(
    (state, action) => {
      if (!action || !action.type || ! (action.type in reducers)) {
        console.error('Action not registered'); 
        console.error(action); 
        console.error(action.type); 
        return state; 
      }

      return reducers[action.type].call(null, state, action);
    },
    initialState || {}
  )).pipe(share());

  const stateSymbol = Symbol('state');
  store.subscribe(state => {
    store[stateSymbol] = state;
  }, (err) => {
    console.error('Error in the store', err);
  });
  store.subscribe = () => store[stateSymbol];

  function dispatch(action) {
    return typeof action === 'function'
      ? action.call(null, subject, dispatch)
      : subject.next(action);
  }

  return {
    store,
    dispatch,
  };
}
0 Answers
Related