Well, the forFeature() method is a generic as well, and we can specify this one explicitly like so:
// counter.module.ts
const CounterState = {
data: number
};
const reducers: ActionReducerMap<CounterState> = {
data: counterReducer
}
@NgModule({
imports: [
StoreModule.forFeature<CounterState, Action>('counter', reducers)
]
})
This protects us from adding a state mapping object that it does not expect to the forFeature() method. For instance, the following would render an error:
// example of what NOT to do
interface State {
test: string;
}
function testReducer(state ="", action: Action) {
switch(action.type) {
default:
return state;
}
}
const reducers: ActionReducerMap<State> = {
test: testReducer
};
@NgModule({
imports: [
BrowserModule,
StoreModule.forFeature<CounterState, Action>('counter', reducers)
],
exports: [CounterComponent, CounterListComponent],
declarations: [CounterComponent, CounterListComponent],
providers: [],
})
export class CounterModule { }
The reason for this is that we are providing the wrong type to the forFeature() method. It expects a reducer parameter to be something of type ActionReducerMap<CounterState>, which it clearly is not, as we are sending in ActionReducerMap<State>.