title: Typescript in redux reducer
body: <p>Following <a href="https://redux.js.org/recipes/usage-with-typescript" rel="nofollow noreferrer">redux guide to typescript</a> I came up with the following code for my reducer. Typescript can't infer the right types on the switch case statement, though. </p>
  
  <p>Reading <a href="https://stackoverflow.com/a/40758619/7502752">this answer</a> and its pointed <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions" rel="nofollow noreferrer">typescript docs</a>, I was able to make it work using string literals on the type variable on the interface. But I don't wanna use literals in there, since I am exporting my actions strings in a Actions variable, like in the code below.   </p>
  
  <p>What am I missing to be able to make it work like on the redux guide?</p>
  
  <pre class="lang-js prettyprint-override"><code>export const Action = {
    action1:'ACTION1',
    action2:'ACTION2'
  }
  
  interface actionType1 {
    type: typeof Action.action1, 
    stringPayload:string
  }
  
  interface actionType2 {
    type: typeof Action.action2,
    objectPayload:{
      str: string
    } 
  }
  
  type actionType = actionType1 | actionType2;
  
  interface stateType {
    str:string,
  }
  
  const defaultState = {
    str:''
  };
  
  
  const reducer = ( state = defaultState, action:actionType) : stateType =&gt; {
    switch(action.type){
      case Action.action1:
        return {
          str:action.stringPayload //stringPayload appears as error
        }
          /*
          [ts]
          Property 'stringPayload' does not exist on type 'actionType'.
          Property 'stringPayload' does not exist on type 'actionType2'.
          */ 
  
      case Action.action2:
        return action.objectPayload //objectPayload appears as error
        /*
        [ts]
        Property 'objectPayload' does not exist on type 'actionType'.
        Property 'objectPayload' does not exist on type 'actionType1'
        */
  
      default:
        return state  
    }
  }
  </code></pre>