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

Conversation

timdeschryver
Copy link
Member

Had to create Actions instead of types to make ActionReducerMap happy.

Closes #709

Copy link
Member Author

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

Not related to this PR but I see the actions from router store module are named RouterAction, wouldn't it be better to use RouterActions?

@@ -96,7 +96,7 @@ export type RouterReducerState<T = RouterStateSnapshot> = {
};

export function routerReducer<T = RouterStateSnapshot>(
state: RouterReducerState<T>,
state: RouterReducerState<T> | undefined,
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to use undefined because the signature for an ActionReducer is (state: T | undefined, action: V)

@@ -39,14 +40,14 @@ export interface State {
* These reducer functions are called with each dispatched action
* and the current or initial state and return a new immutable state.
*/
export const reducers: ActionReducerMap<State> = {
export const reducers: ActionReducerMap<State, any> = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not quite happy with any but I think this is the only way to do it.
Feedback welcome!

Copy link
Contributor

Choose a reason for hiding this comment

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

Although this will fix router store but users still have to manually add ‘any’ by themself to by pass typescript check in their store. we need a better approach

Copy link
Member Author

Choose a reason for hiding this comment

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

What about the last commit? I think we're getting closer...

@coveralls
Copy link

coveralls commented Feb 6, 2018

Coverage Status

Coverage decreased (-0.2%) to 92.493% when pulling 92646fc on tdeschryver:pr/router-actions into 0429276 on ngrx:master.

export class RouterNavigationAction<T = RouterStateSnapshot> implements Action {
readonly type = ROUTER_NAVIGATION;
constructor(public payload: RouterNavigationPayload<T>) {}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

after typescript 2.8 landed which support conditional type, I think ngrx team will stick with interface based Action

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I did not know about this!
But since we're still in 2.7 this is the only way to do it.

Copy link
Member

Choose a reason for hiding this comment

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

If we need to use classes here, make them abstract

@brandonroberts
Copy link
Member

Does this PR only cover support for router-store with TS 2.6 and Angular 5.2?

@@ -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 class RouterNavigationAction<T = RouterStateSnapshot> implements Action {
readonly type = ROUTER_NAVIGATION;
constructor(public payload: RouterNavigationPayload<T>) {}
}
Copy link
Member

Choose a reason for hiding this comment

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

If we need to use classes here, make them abstract

@timdeschryver
Copy link
Member Author

I'm thinking TS 2.4.2 and up but I'm not 100% sure.
I'll run some tests tomorrow.

@timdeschryver
Copy link
Member Author

timdeschryver commented Feb 9, 2018

So, if I'm not mistaken it should work with TS 2.4.2 and up.
But I do get compile errors on another part (even on the master branch) when I upgraded TS to 2.6.1 and 2.7.1.

TSError: ⨯ Unable to compile TypeScript modules\store\src\reducer_manager.ts (68,5): Type 'Partial<ActionReducerMap<any, any>>' is not assignable to type 'ActionReducerMap<any, any>'. Index signatures are incompatible. Type 'ActionReducer<any, any> | undefined' is not assignable to type 'ActionReducer<any, any>'. Type 'undefined' is not assignable to type 'ActionReducer<any, any>'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants