Consider the following ngrx objects:
export class ToggleTheme implements Action {
public static readonly typeId = "[Theme Service] Toggle";
readonly type = ToggleTheme.typeId;
}
export function selectedThemeReducer(state: ThemeState = initialState, action: ToggleTheme) {
switch (action.type){
case ToggleTheme.typeId:
return {
state,
current: state.current == "DARK" ? "LIGHT" : "DARK"
};
default:
return state;
}
}
@NgModule({
imports: [
StoreModule.forRoot({
theme: selectedThemeReducer
}, {});
]
})
export class AppModule {}
Typescript is complaining in the module imports:
TS2322: Type '(state: ThemeState | undefined, action: ToggleTheme) => ThemeState | { state: ThemeState; current: string; }' is not assignable to type 'ActionReducer<ThemeState | { state: ThemeState; current: string; }, Action>'. Types of parameters 'action' and 'action' are incompatible. Type 'Action' is not assignable to type 'ToggleTheme'. Types of property 'type' are incompatible. Type 'string' is not assignable to type '"[Theme Service] Toggle"'.
If I switch the reducer parameter back to Action like below, the issue goes away - but I degrade the type and lose autocomplete. No big deal - it works, but just trying to get better at typescript and want to understand what the problem is. I think I could relax things with @ts-ignore but that makes me nervous being a C# guy 🙂
export function selectedThemeReducer(state: ThemeState = initialState, action: Action)