Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
Typescript Redux Type error of action.payload in reducer file

I am getting a type error in my reducers' file that my Property 'address' does not exist on type 'number | { connection: boolean; address: string; }'. I am using redux with typescript in my react application. I know it is due to the incorrect typesetting but I can't figure out how. I am using typescript with redux the first time and got stuck in this
Here is my actions file.

interface IMetaMaskConnection{
    type:typeof ActionType.CONNECT_META_MASK,
     payload:{
       connection:boolean,
       address:string
     }
}
interface IHourPassed{
  type:typeof ActionType.HOUR_PASSED,
  payload:number
}


export type Action = IMetaMaskConnection | IHourPassed 

export const connectMetaMaskAction = (data:IMetaMaskConnection['payload']):Action => ({
  type: ActionType.CONNECT_META_MASK,
 payload:data
});
export const setHourPassed = (data:IHourPassed['payload']):Action => ({
  type: ActionType.HOUR_PASSED,
 payload:data
});


This is my reducer.
export const reducer= (state:IState=initialState, action:Action):IState=> {
    const {type, payload}=action;
    switch(type){
        case ActionType.CONNECT_META_MASK:
        return {
        ...state,
        address:payload.address,
        connection:payload.connection
        } 
        case ActionType.HOUR_PASSED:
        return {
        ...state,
        hourPassed:payload
        } 
        
         default:
      return state;
    }
}
export type State= ReturnType<typeof reducer>

Error screen shot


This is my error screenshot.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first issue here is that you should really be using our official Redux Toolkit package and its createSlice API for writing your reducers. createSlice will also generate action creators for you automatically.

See the Redux TS Quick Start guide for details on how to set up and use RTK with TypeScript.

Beyond that, the actual error here is that you told TS "the action can be one of these two types", but the destructuring of {type} means that TS can't narrow down the TS type of the action object inside the case statement (although TS 4.5 did just improve that behavior). So, TS still thinks it could be either of those two action types.

But all those problems would go away if you switch to using createSlice.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda