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(store): allow a generic reducer #2996

Merged
merged 1 commit into from
Apr 19, 2021
Merged
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
19 changes: 18 additions & 1 deletion modules/store/spec/types/reducer_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compilerOptions } from './utils';
describe('createReducer()', () => {
const expectSnippet = expecter(
(code) => `
import {createAction, createReducer, on, props} from '@ngrx/store';
import {createAction, createReducer, on, props, ActionCreator, ActionReducer} from '@ngrx/store';

${code}
`,
Expand Down Expand Up @@ -57,6 +57,23 @@ describe('createReducer()', () => {
);
`).toInfer('reducer', 'ActionReducer<number, Action>');
});

it('should support a generic reducer factory', () => {
expectSnippet(`
const creator = null as unknown as ActionCreator;

export function createGenericReducer<TState extends object>(initialState: TState): ActionReducer<TState> {
const reducer = createReducer(
initialState,
on(creator, (state, action) => {
return state ;
})
);

return reducer;
}
`).toSucceed();
});
});

describe('on()', () => {
Expand Down
11 changes: 6 additions & 5 deletions modules/store/src/reducer_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export interface ReducerTypes<

// Specialized Reducer that is aware of the Action type it needs to handle
export interface OnReducer<State, Creators extends readonly ActionCreator[]> {
(state: State, action: ActionType<Creators[number]>): State extends object
? { [P in keyof State]: State[P] }
: State;
(state: State, action: ActionType<Creators[number]>): State;
}

/**
Expand All @@ -42,11 +40,14 @@ export interface OnReducer<State, Creators extends readonly ActionCreator[]> {
* ```
*/
export function on<State, Creators extends readonly ActionCreator[]>(
...args: [...creators: Creators, reducer: OnReducer<State, Creators>]
...args: [
...creators: Creators,
reducer: OnReducer<State extends infer S ? S : never, Creators>
]
): ReducerTypes<State, Creators> {
// This could be refactored when TS releases the version with this fix:
// https://github.com/microsoft/TypeScript/pull/41544
const reducer = args.pop() as OnReducer<State, Creators>;
const reducer = args.pop() as OnReducer<any, Creators>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const reducer = args.pop() as OnReducer<any, Creators>;
const reducer = args.pop() as OnReducer<unknown, Creators>;

Would that still work? 🙂

const types = (((args as unknown) as Creators).map(
(creator) => creator.type
) as unknown) as ExtractActionTypes<Creators>;
Expand Down