Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property id does not exist on type 'never'

I'm trying to do a .map function into a state array in Redux with Typescript, the problem is that it's throwing an error

[ts] Property 'id' does not exist on type 'never'

in the landing.id if statement, since its an array of objects the code makes sense for me but seems that I'm missing something there

export default function landingReducer (state = [], action) {
switch(action.type) {
  case ActionTypes.ADD_LANDING:
    return [...state, action.landing]
  case ActionTypes.EDIT_LANDING:
    return state.map(landing => {
      if (action.landing.id == landing.id) {
        return landing;
      }
    })

Thanks in advance!

like image 380
Lucas Janon Avatar asked Oct 19 '25 14:10

Lucas Janon


1 Answers

It's probably due to types missing for the state and/or action arguments. Try this code that should work with TypeScript 2.4+, inspired by this article:

interface Landing {
    id: any;
}

enum ActionTypes {
    ADD_LANDING  = "ADD_LANDING",
    EDIT_LANDING = "EDIT_LANDING",
    OTHER_ACTION = "__any_other_action_type__"
}

interface AddLandingAction {
    type: ActionTypes.ADD_LANDING;
    landing: Landing;
}

interface EditLandingAction {
    type: ActionTypes.EDIT_LANDING;
    landing: Landing;
}

type LandingAction =
    | AddLandingAction
    | EditLandingAction;

function landingReducer(state: Landing[], action: LandingAction) {
    switch (action.type) {
        case ActionTypes.ADD_LANDING:
            return [...state, action.landing]

        case ActionTypes.EDIT_LANDING:
            return state.map(landing => {
                if (action.landing.id === landing.id) {
                    return landing;
                }
            });
    }
}
like image 155
Romain Deneau Avatar answered Oct 21 '25 02:10

Romain Deneau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!