Skip to content

Commit

Permalink
[redux] Fix Partial error on newer flow versions (#4457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brianzchen committed Jun 13, 2023
1 parent c775422 commit 0e28de5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const legacyStore9: Store = legacy_createStore(reducer, undefined, myEnhancer);
//

const s: State = store2.getState()
// $FlowExpectedError[incompatible-type]
// $FlowExpectedError[name-already-bound]
const s: number = store2.getState() // wrong return type

Expand Down
11 changes: 3 additions & 8 deletions definitions/npm/redux_v4.x.x/flow_v0.201.x-/redux_v4.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ declare module 'redux' {

declare export type Reducer<S, A> = (state: S, action: A) => S;

declare export type CombinedReducer<S, A> = (
state: ($Shape<S> & {...}) | void,
action: A
) => S;

declare export type Middleware<S, A, D = Dispatch<A>> = (
api: MiddlewareAPI<S, A, D>
) => (next: D) => D;
Expand Down Expand Up @@ -98,9 +93,9 @@ declare module 'redux' {
dispatch: D
): C;

declare export function combineReducers<O: {...}, A>(
reducers: O
): CombinedReducer<$ObjMap<O, <S>(r: Reducer<S, any>) => S>, A>;
declare export function combineReducers<RootState: {...}, A>(
reducers: $ObjMap<RootState, <V>(V) => Reducer<V, A>>,
): Reducer<RootState, A>;

declare export var compose: $Compose;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import type { Store as ReduxStore, MiddlewareAPI } from 'redux'
import type { Store as ReduxStore, MiddlewareAPI, Dispatch } from 'redux'
import { applyMiddleware, createStore } from 'redux'

type State = Array<number>;
Expand All @@ -11,17 +11,17 @@ const reducer = (state: State = [], action: Action): State => state
// applyMiddleware API
//

applyMiddleware();
applyMiddleware(api => next => next);
applyMiddleware<State, Action, Dispatch<Action>>();
applyMiddleware<State, Action, Dispatch<Action>>(api => next => next);
// $FlowExpectedError[incompatible-call]
applyMiddleware('wrong');

//
// interaction with createStore
//

createStore(reducer, [1], applyMiddleware(api => next => next))
createStore(reducer, [1], applyMiddleware((api: MiddlewareAPI<State, Action>) => {
createStore(reducer, [1], applyMiddleware<State, Action, Dispatch<Action>>(api => next => next))
createStore(reducer, [1], applyMiddleware<State, Action, Dispatch<Action>>((api: MiddlewareAPI<State, Action>) => {
// $FlowExpectedError[incompatible-type]
const s: number = api.getState() // wrong return type
// $FlowExpectedError[incompatible-call]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ const reducer0: Reducer<State, Action> = combineReducers({
})

// $FlowExpectedError[incompatible-call]
combineReducers() // wrong reducers argument
combineReducers<State, Action>() // wrong reducers argument
// $FlowExpectedError[incompatible-call]
combineReducers([]) // wrong reducers argument
combineReducers<State, Action>(([]: Array<any>)) // wrong reducers argument

// $FlowExpectedError[prop-missing]
// $FlowExpectedError[incompatible-type]
const reducer1: Reducer<State, Action> = combineReducers({
a: reducerA,
name: reducerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ const reducer = (state: State = [], action: Action): State => state
// createStore API
//

// $FlowExpectedError[incompatible-type-arg]
// $FlowExpectedError[incompatible-call]
const store1: Store = createStore(() => ({})); // wrong reducer
const store2: Store = createStore(reducer);
// $FlowExpectedError[incompatible-call]
const store3: Store = createStore(reducer, {}); // wrong initialState shape
const store4: Store = createStore(reducer, []);
// $FlowExpectedError[incompatible-type]
// $FlowExpectedError[incompatible-call]
const store5: Store = createStore(reducer, ['wrong']); // wrong initialState content
const store6: Store = createStore(reducer, [1]);
Expand All @@ -28,14 +26,12 @@ declare var myEnhancer: StoreEnhancer<State, Action>;
const store8: Store = createStore(reducer, [1], myEnhancer);
const store9: Store = createStore(reducer, undefined, myEnhancer);

// $FlowExpectedError[incompatible-type-arg]
// $FlowExpectedError[incompatible-call]
const legacyStore1: Store = legacy_createStore(() => ({})); // wrong reducer
const legacyStore2: Store = legacy_createStore(reducer);
// $FlowExpectedError[incompatible-call]
const legacyStore3: Store = legacy_createStore(reducer, {}); // wrong initialState shape
const legacyStore4: Store = legacy_createStore(reducer, []);
// $FlowExpectedError[incompatible-type]
// $FlowExpectedError[incompatible-call]
const legacyStore5: Store = legacy_createStore(reducer, ['wrong']); // wrong initialState content
const legacyStore6: Store = legacy_createStore(reducer, [1]);
Expand All @@ -50,7 +46,6 @@ const legacyStore9: Store = legacy_createStore(reducer, undefined, myEnhancer);
//

const s: State = store2.getState()
// $FlowExpectedError[incompatible-type]
// $FlowExpectedError[name-already-bound]
const s: number = store2.getState() // wrong return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ ac1(1);
// $FlowExpectedError[incompatible-call]
bindActionCreators((n: number) => ({ type: 'wrong' }), dispatch); // wrong action

const ac2 = bindActionCreators((n: number) => (dispatch, getState) => {}, dispatch);
const ac2 = bindActionCreators((n: number) => (dispatch: Dispatch, getState: () => State) => {}, dispatch);
ac2(1);
// $FlowExpectedError[incompatible-call]
bindActionCreators((n: number) => (dispatch, getState) => 'wrong', dispatch); // wrong thunk
bindActionCreators((n: number) => (dispatch: Dispatch, getState: () => State) => 'wrong', dispatch); // wrong thunk

//
// createStore API
Expand Down

0 comments on commit 0e28de5

Please sign in to comment.