omichelsen / redux-promise-middleware-actions

Redux action creator for making async actions compatible with redux-promise-middleware
MIT License
32 stars 3 forks source link

createReducer allows setting unknown fields to new state #17

Open sregg opened 4 years ago

sregg commented 4 years ago

image

See line 17 nane: action.payload.name, TS is nit complaining about that I typed nane instead of name.

If I add the type manually:

image

Then I see the error fine.

Is there a way to have the error without the manual type?

Thanks

sregg commented 4 years ago

Here's the code to repro:

import { createAction, createReducer } from 'redux-promise-middleware-actions';

interface IState {
  name: string;
}

const INITIAL_STATE: IState = {
  name: '',
};

const setName = createAction('SET_NAME', (name: string) => ({ name }));

export const NameReducer = createReducer(INITIAL_STATE, handleAction => [
  // intro screen
  handleAction(setName, (state, action) => ({
    ...state,
    nane: action.payload.name,
  })),
]);
omichelsen commented 4 years ago

Good point, I will try and add this. If you have a solution a PR is of course very welcome :)

Currently it would actually give you a warning if you only set the name property, but because you are spreading the state (...state) it has already set name (to the old value) and will not warn you because you are allowed to extend the object with a new nane prop.