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

fix(RouterStore): type mismatch #794

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions example-app/app/core/reducers/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const initialState: State = {
showSidenav: false,
};

export function reducer(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed for router-store?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the example app will have the following error when we create the ActionReducerMap:
Type '{ layout: (state: State | undefined, action: LayoutActions) => State; router: <V extends RouterAc...' is not assignable to type 'ActionReducerMap<State, Action>'.

export function reducer<T extends LayoutActions>(
state: State = initialState,
action: LayoutActions
action: T
): State {
switch (action.type) {
case LayoutActionTypes.CloseSidenav:
Expand Down
3 changes: 2 additions & 1 deletion example-app/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createFeatureSelector,
ActionReducer,
MetaReducer,
Action,
} from '@ngrx/store';
import { environment } from '../../environments/environment';
import { RouterStateUrl } from '../shared/utils';
Expand Down Expand Up @@ -46,7 +47,7 @@ export const reducers: ActionReducerMap<State> = {

// console.log all actions
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
return function(state: State, action: any): State {
return function(state: State | undefined, action: any): State {
console.log('state', state);
console.log('action', action);

Expand Down
39 changes: 21 additions & 18 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RouterStateSnapshot,
RoutesRecognized,
} from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Store, select, Action } from '@ngrx/store';
import { of } from 'rxjs/observable/of';
import {
DefaultRouterStateSerializer,
Expand All @@ -33,11 +33,12 @@ export type RouterNavigationPayload<T> = {
/**
* An action dispatched when the router navigates.
*/
export type RouterNavigationAction<T = RouterStateSnapshot> = {
type: typeof ROUTER_NAVIGATION;
payload: RouterNavigationPayload<T>;
};

export abstract class RouterNavigationAction<T = RouterStateSnapshot>
implements Action {
readonly type = ROUTER_NAVIGATION;
constructor(public payload: RouterNavigationPayload<T>) {}
}
/**
* An action dispatched when the router cancels navigation.
*/
Expand All @@ -55,10 +56,11 @@ export type RouterCancelPayload<T, V> = {
/**
* An action dispatched when the router cancel navigation.
*/
export type RouterCancelAction<T, V = RouterStateSnapshot> = {
type: typeof ROUTER_CANCEL;
payload: RouterCancelPayload<T, V>;
};
export abstract class RouterCancelAction<T, V = RouterStateSnapshot>
implements Action {
readonly type = ROUTER_CANCEL;
constructor(public payload: RouterCancelPayload<T, V>) {}
}

/**
* An action dispatched when the router errors.
Expand All @@ -77,10 +79,11 @@ export type RouterErrorPayload<T, V> = {
/**
* An action dispatched when the router errors.
*/
export type RouterErrorAction<T, V = RouterStateSnapshot> = {
type: typeof ROUTER_ERROR;
payload: RouterErrorPayload<T, V>;
};
export abstract class RouterErrorAction<T, V = RouterStateSnapshot>
implements Action {
readonly type = ROUTER_ERROR;
constructor(public payload: RouterErrorPayload<T, V>) {}
}

/**
* An union type of router actions.
Expand All @@ -95,10 +98,10 @@ export type RouterReducerState<T = RouterStateSnapshot> = {
navigationId: number;
};

export function routerReducer<T = RouterStateSnapshot>(
state: RouterReducerState<T>,
action: RouterAction<any, T>
): RouterReducerState<T> {
export function routerReducer<
V extends RouterAction<any, T>,
T = RouterStateSnapshot
>(state: RouterReducerState<T> | undefined, action: V): RouterReducerState<T> {
switch (action.type) {
case ROUTER_NAVIGATION:
case ROUTER_ERROR:
Expand All @@ -108,7 +111,7 @@ export function routerReducer<T = RouterStateSnapshot>(
navigationId: action.payload.event.id,
};
default:
return state;
return state as RouterReducerState<T>;
}
}

Expand Down