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

Use same return type for both StoreCreator signatures in TS typings #2985

Merged
merged 1 commit into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
*/
export interface StoreCreator {
<S, A extends Action, Ext, StateExt>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<Ext, StateExt>): Store<S & StateExt, A> & Ext;
<S, A extends Action, Ext, StateExt>(reducer: Reducer<S, A>, preloadedState: DeepPartial<S>, enhancer?: StoreEnhancer<Ext>): Store<S & StateExt> & Ext;
<S, A extends Action, Ext, StateExt>(reducer: Reducer<S, A>, preloadedState: DeepPartial<S>, enhancer?: StoreEnhancer<Ext>): Store<S & StateExt, A> & Ext;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type State = {
};
}

interface DerivedAction extends Action {
type: 'a',
b: 'b',
}

const reducer: Reducer<State> = (state: State | undefined = {
a: 'a',
b: {
Expand All @@ -22,6 +27,18 @@ const reducer: Reducer<State> = (state: State | undefined = {
return state;
};

const reducerWithAction: Reducer<State, DerivedAction> = (state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd',
},
}, action: DerivedAction): State => {
return state;
};

const funcWithStore = (store: Store<State, DerivedAction>) => {};

/* createStore */

const store: Store<State> = createStore(reducer);
Expand All @@ -30,6 +47,13 @@ const storeWithPreloadedState: Store<State> = createStore(reducer, {
b: {c: 'c'}
});

const storeWithActionReducer = createStore(reducerWithAction);
const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, {
b: {c: 'c'},
});
funcWithStore(storeWithActionReducer);
funcWithStore(storeWithActionReducerAndPreloadedState);

const enhancer: StoreEnhancer = next => next;

const storeWithSpecificEnhancer: Store<State> = createStore(reducer, enhancer);
Expand Down