Skip to content

Typescript: Handler not returning state #109

@phsantiago

Description

@phsantiago

Hi there, first issue here.

These days using redux-act, I found that the handler of reducer.on isn't using state type received in createReducer.

Example:

interface State {
  loading: boolean;
  name?: string;
  office?: office;
  claims?: Array<Claim>;
}

const reducer = createReducer<State>({}, initialState);

reducer.on(loading, (state, payload) => ({
  ...state,
  loading: payload,
  foo: 'bar',
}));

foo: 'bar' isn't in my interface, and don't throw an error in my typings.

It only works when I explict use State interface as result of my handler function.
Example:

reducer.on(loading, (state, payload): State => ({
  ...state,
  loading: payload,
  foo: 'bar',
}));

Only this way I got the expected behaviour:

src/modules/user.ts|31 col 3 error| 2322[QF available]: Type '{ loading: boolean; foo: string; name?: string | undefined; escritorio?: Escritorio | undefined; claims?: Claim[] | undefined; }' is not assignable to type 'State'. Object literal may only specify known properties, and 'foo' does not exist in type 'State'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions