angular - Typescript, @ngrx and default store values -
i think missing obvious trying work @ngrx. trying use nested reducers, , want able return default starting child values. child
export const counter:reducer<number> = (state:number = 0, action:action = {type:null}) => { switch (action.type) {
my question how use initialise counter. counters.ts looks like
export const counters:reducer<number[]> = (state:number[] = [], action:action) => { switch (action.type) { case add_counter: return [counter(), ...state];
this works have typescript error on counter: supplied parameters not match signature of call target
, i'm wondering how around this.
i'm using @ngrx, provides this:
export interface action { type: string; payload?: any; } export interface reducer<t> { (state: t, action: action): t; } export class store<t> extends behaviorsubject<t> { private _storesubscription: subscription<t> constructor(private _dispatcher:dispatcher<action>, private _reducers:{[key:string]:reducer<any>}, initialstate: t) { super(initialstate); let rootreducer = this._mergereducers(_reducers, initialstate); this._storesubscription = rootreducer.subscribe(this); }
the definition of reducer
should be
export interface reducer<t> { (state?: t, action?: action): t; }
to allow calling without parameters.
default parameter values allowed in constructor , function implementations (like in lambda), can't specify default values in interface definition.
when call counter()
typescript compiler check type (reducer<number>
) , see 2 parameters required.
so either suggested above or call default parameters:
counter(0, {type: null})
Comments
Post a Comment