Skip to content

Broken type of on function when created outside of createReducer factory function #3576

Closed
@alex-okrushko

Description

@alex-okrushko

Which @ngrx/* package(s) are the source of the bug?

store

Minimal reproduction of the bug/regression with instructions

(Discovered as a result of the attempted NgRx merge into g3)

Since #2982 when on function was created outside of the createReducer, the State type is being incorrectly inferred as unknown.

E.g. the following code:

    interface State { name: string };
    const foo = createAction('FOO', props<{ foo: string }>());
    const onFn = on(foo, (state: State, action) => ({  name: action.foo }));

would produce the following error:

Argument of type '(state: State, action: { foo: string; } & TypedAction<"FOO"> & { type: "FOO"; }) => { name: string; }' 
is not assignable to parameter of type 'OnReducer<unknown, [ActionCreator<"FOO", (props: { foo: string; }) => { foo: string; } & TypedAction<"FOO">>]>'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'unknown' is not assignable to type 'State'

Minimal reproduction of the bug/regression with instructions

on function should correctly infer the State type that's specified explicitly in the OnReducer function.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

NgRx: latest 14.3.1

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions