rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.97k stars 867 forks source link

Persistor flow definition incompatible with redux_v3.x.x.js flow-typed createStore definition #823

Open Fsarmento opened 6 years ago

Fsarmento commented 6 years ago

In node_modules/redux-persist/lib/persistStore.js.flow:94:30, persistor is defined as

let persistor: Persistor = {
    ..._pStore,
    purge: () => {

The problem here is that Persistor defines dispatch, getState and subscribe as read-only, while _pStore imports the flow-types of these 3 properties from 'redux'. Flow-typed defines the same properties as writable for 'redux' library.

This causes flow to throw 3 erros: $ flow Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ node_modules/redux-persist/lib/persistStore.js.flow:94:30

Property dispatch is not writable. Property getState is not writable. Property subscribe is not writable.

More detail

In persistStore.js.flow, _pStore is defined using createStore flow-typed definition imported from 'redux

import { createStore } from 'redux'

let _pStore = createStore(persistorReducer, initialState, options ? options.enhancer : undefined)

On redux_v3.x.x.js, createStore returns a Store type where dispatch, getState and subscribe are writable:

declare export function createStore<S, A, D>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<S, A, D>): Store<S, A, D>;

declare export type Store<S, A, D = Dispatch<A>> = {
    // rewrite MiddlewareAPI members in order to get nicer error messages (intersections produce long messages)
    dispatch: D;
    getState(): S;
    subscribe(listener: () => void): () => void;
    replaceReducer(nextReducer: Reducer<S, A>): void
  };

However, the flow type "Persistor" on persistStore.js is read only:

export type Persistor = {
  pause: () => void,
  persist: () => void,
  purge: () => Promise<any>,
  flush: () => Promise<any>,
  +dispatch: PersistorAction => PersistorAction,
  +getState: () => PersistorState,
  +subscribe: PersistorSubscribeCallback => () => any,
}
deecewan commented 6 years ago

Is there a fix happening here? I'm not really sure why this lib shouldn't just follow the redux API.