Use external constant when defining stream literal type

I would like to use an imported constant from a package in a stream literal type and a static check of a switch;

Is there a way to do this? (example below)

// action/types.js
import { REHYDRATE } from 'redux-persist/constants'

export type FooBar = {
  foo: number,
  bar: string,
};

export type Action
  = { type: 'FETCH_REQUEST', data: FooBar[] }
  | { type: REHYDRATE, payload: any } // <== this do not work
  ;

// reducer.js
import { REHYDRATE } from 'redux-persist/constants'

export default function (state: State = initialState, action: Action) 
    switch (action.type) {
    case 'FETCH_REQUEST': 
        // do something with action.data
    case REHYDRATE: { // <= flow says: uncovered code
        // do something with action.payload
    default:
        return state
    }
}

      

+3


source to share


1 answer


Flow

does not support the use of variables containing constants in type definitions.

You must either use a string value or a support datatype in your definition.



This post was a similar problem if you'd like to view it as well.

+4


source







All Articles