-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
metaReducer typing expects ActionReducer array #170
Comments
BTW, defining meta reducers like this also lead to AOT issues:
Does anyone know how to get around this issue? |
You can't use anonymous arrow functions with AOT. The function has to be a named export. Meta reducers take a reducer and return a new reducer function like the debug example. The composition is from right to left on the items in the array. Underneath, export function logger(reducer): ActionReducer<any, any> {
return function(state, action) {
console.log('state', state);
console.log('action', action);
return reducer(state, action);
};
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
StoreModule.forRoot(REDUCERS_TOKEN, {
metaReducers: [
// (...args) => console.log(args)
logger
]
})
],
providers: [
{ provide: REDUCERS_TOKEN, useValue: reducers }
],
bootstrap: [AppComponent]
})
export class AppModule { } |
It works indeed when the logger is moved to a separate module. I don't know why the typescript compiler doesn't complain about it as from what I see the types are still incompatible. It originally stumbled upon this issue when trying to integrate btroncone/ngrx-store-logger which exposes the same API when calling storeLogger() export declare const storeLogger: (opts?: LoggerOptions) => (reducer: Function) => (state: any, action: any) => any; So even though logger from the example above as well as the return value from storeLogger() call return the same type, the latter fails with the error mentioned above. I updated the repo to reflect this. |
The same rule still applies with static analysis, where the code needs to be read without being executed. In order to use import { storeLogger } from 'ngrx-store-logger';
// console.log all actions
export function logger(reducer: ActionReducer<State>): ActionReducer<any, any> {
return storeLogger()(reducer);
}
export const metaReducers: ActionReducer<any, any>[] = !environment.production
? [logger]
: []; |
@brandonroberts I am also confused by the type signature. Could you explain why it is correct? I feel like a meta reducer should be a function that takes in an |
For some reason, we need to manually specify the type of the meta-reducers, as shown in ngrx/platform#170 (comment) Fixes btroncone#45
I created this issue before actually noticing the last comment here |
BTW I was able to make our app work with this meta-reducer: function consoleLogEnhancer(reducer) {
function loggingReducer(state, action) {
//console.log('state', state);
console.log('action', action);
return reducer(state, action);
}
return loggingReducer;
} while the following causes errors at runtime, it seems like is messing up the state passed around: function storeLoggerEnhancer(reducer) {
function storeLoggingReducer(state, action) {
const opts = {
//timestamp: false,
};
return storeLogger(opts)(reducer);
}
return storeLoggingReducer;
} |
@brandonroberts when adding the meta reducer as you mentioned I get the following error: this started to happen when using inject tokens for the reducers and updating to the latest version of ngrx. When using the following meta reducer:
the meta reducer is called with action undefined: My meta reducers: App-Module: Any suggestions? |
* Update readme for ngrx 4.0 For some reason, we need to manually specify the type of the meta-reducers, as shown in ngrx/platform#170 (comment) Fixes #45 * Fix readme for ngrx store 4.0 uses fix from #43
@saulshanabrook @GiuseppePiscopo the type signature was incorrect for the meta-reducer array and has been resolved here #270 |
@brandonroberts I got the latest version including the MetaReducer type and use the following code:
It fails with the same error: |
thx @brandonroberts, it seems to be fixed |
Any ideas on that btroncone/ngrx-store-localstorage#59 ? |
This issue appears to still be around in version |
I keep running into this type check error TS2345 CODE: index.ts (for state)
core.module.ts:
ERROR:
|
According to the documentation, the
metaReducer
property of the store configuration expects an array of functions that accept the reducer and return a reducer function.https://github.com/ngrx/platform/blob/master/docs/store/api.md#meta-reducers
I tested this using this code...
... and the browser console confirms this:
However, when I want to actually implement the code from the documentation, I get typing issues.
From what I see, the
StoreConfig
interface incorrectly definesmetaReducers
asActionReducer<any, any>[];
platform/modules/store/src/models.ts
Line 29 in d2295c7
From what I understand the correct typing should be
ActionReducer<T, V>[]
.Can anyone confirm this?
Update
Here's a repo to reproduce this issue:
https://github.com/luchsamapparat/ngrx-meta-reducer-issue
Just run
npm install
and thennpm start
The text was updated successfully, but these errors were encountered: