Skip to content

Commit 57633d2

Browse files
fix(Store): Add type signature for metareducer (#270)
Closes #264, Related to #170
1 parent 9e33a5d commit 57633d2

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

docs/store/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export function getInitialState() {
4949
configuration option to provide an array of meta-reducers that are composed from right to left.
5050

5151
```ts
52-
import { StoreModule, combineReducers, compose } from '@ngrx/store';
52+
import { StoreModule, ActionReducer } from '@ngrx/store';
5353
import { reducers } from './reducers';
5454

5555
// console.log all actions
56-
function debug(reducer) {
56+
export function debug(reducer: ActionReducer<any>): ActionReducer<any> {
5757
return function(state, action) {
5858
console.log('state', state);
5959
console.log('action', action);
@@ -62,7 +62,7 @@ function debug(reducer) {
6262
}
6363
}
6464

65-
const metaReducers = [debug];
65+
export const metaReducers = [debug];
6666

6767
@NgModule({
6868
imports: [

example-app/app/reducers/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createSelector,
44
createFeatureSelector,
55
ActionReducer,
6+
MetaReducer,
67
} from '@ngrx/store';
78
import { environment } from '../../environments/environment';
89
import * as fromRouter from '@ngrx/router-store';
@@ -36,7 +37,7 @@ export const reducers: ActionReducerMap<State> = {
3637
};
3738

3839
// console.log all actions
39-
export function logger(reducer: ActionReducer<State>): ActionReducer<any, any> {
40+
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
4041
return function(state: State, action: any): State {
4142
console.log('state', state);
4243
console.log('action', action);
@@ -50,7 +51,7 @@ export function logger(reducer: ActionReducer<State>): ActionReducer<any, any> {
5051
* the root meta-reducer. To add more meta-reducers, provide an array of meta-reducers
5152
* that will be composed to form the root meta-reducer.
5253
*/
53-
export const metaReducers: ActionReducer<any, any>[] = !environment.production
54+
export const metaReducers: MetaReducer<State>[] = !environment.production
5455
? [logger]
5556
: [];
5657

modules/store/spec/utils.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
combineReducers,
66
compose,
77
createReducerFactory,
8+
MetaReducer
89
} from '@ngrx/store';
910

1011
describe(`Store utils`, () => {
@@ -88,7 +89,7 @@ describe(`Store utils`, () => {
8889
const initialState: FruitState = { fruit: 'apple' };
8990

9091
const runWithExpectations = (
91-
metaReducers: any[],
92+
metaReducers: MetaReducer<FruitState>[],
9293
initialState: any,
9394
expectedState: any
9495
) => () => {

modules/store/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {
33
ActionReducer,
44
ActionReducerMap,
55
ActionReducerFactory,
6+
MetaReducer,
67
Selector,
78
} from './models';
89
export { StoreModule } from './store_module';

modules/store/src/models.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ export interface ActionReducerFactory<T, V extends Action = Action> {
2121
): ActionReducer<T, V>;
2222
}
2323

24+
export type MetaReducer<T, V extends Action = Action> = (
25+
reducer: ActionReducer<T, V>
26+
) => ActionReducer<T, V>;
27+
2428
export interface StoreFeature<T, V extends Action = Action> {
2529
key: string;
2630
reducers: ActionReducerMap<T, V> | ActionReducer<T, V>;
2731
reducerFactory: ActionReducerFactory<T, V>;
2832
initialState?: InitialState<T>;
29-
metaReducers?: ActionReducer<any, any>[];
33+
metaReducers?: MetaReducer<T, V>[];
3034
}
3135

3236
export interface Selector<T, V> {

modules/store/src/store_module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ActionReducerFactory,
1414
StoreFeature,
1515
InitialState,
16+
MetaReducer,
1617
} from './models';
1718
import { compose, combineReducers, createReducerFactory } from './utils';
1819
import {
@@ -82,7 +83,7 @@ export class StoreFeatureModule implements OnDestroy {
8283
export type StoreConfig<T, V extends Action = Action> = {
8384
initialState?: InitialState<T>;
8485
reducerFactory?: ActionReducerFactory<T, V>;
85-
metaReducers?: ActionReducer<T, V>[];
86+
metaReducers?: MetaReducer<T, V>[];
8687
};
8788

8889
@NgModule({})

modules/store/src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ActionReducer,
44
ActionReducerMap,
55
ActionReducerFactory,
6+
MetaReducer,
67
} from './models';
78

89
export function combineReducers<T, V extends Action = Action>(
@@ -84,10 +85,10 @@ export function compose(...functions: any[]) {
8485
};
8586
}
8687

87-
export function createReducerFactory(
88-
reducerFactory: ActionReducerFactory<any, any>,
89-
metaReducers?: ActionReducer<any, any>[]
90-
): ActionReducerFactory<any, any> {
88+
export function createReducerFactory<T, V extends Action = Action>(
89+
reducerFactory: ActionReducerFactory<T, V>,
90+
metaReducers?: MetaReducer<T, V>[]
91+
): ActionReducerFactory<T, V> {
9192
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
9293
return compose(...metaReducers)(reducerFactory) as ActionReducerFactory<
9394
any,

0 commit comments

Comments
 (0)