Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!