My experience with ngrx is relatively new. In my typical TypeScript work, I usually encounter an incorrect assignment error like the one below due to a missing property in the interface declaration:
interface IExample {
count: number;
}
let initialState: IExample = { count: 0, other: '??????' };
// Type '{ count: number; other: string; }' is not assignable to type 'IExample'.
// Object literal may only specify known properties, and 'other' does not exist in type 'IExample'.
However, while working on a reducer using the on
associator (as I understand it's called), this exception is not thrown and other
gets added to the return object without any issues.
interface IExample{
count: number;
}
let initialState: IExample = { count: 0 };
const reducer = createReducer(
initialState,
on(add, (state, { payload }) => ({
...state,
other: '??????'
})),
);
The signature for the on
function is as follows:
on<State, Creators extends readonly ActionCreator[]>(...args: [any, Creators, any, OnReducer<State extends infer S ? S : never, Creators>]): ReducerTypes<State, Creators>
How can I avoid these types of mistakes and ensure that compilation errors are shown as expected?
Note:
,"@ngrx/store": "^12.3.0"