Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use external constant when defining flow literal type

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

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

// 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
    }
}
like image 648
Filipe Borges Avatar asked Jun 29 '17 20:06

Filipe Borges


1 Answers

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

You have to either use the string value itself or a support data type in your definition.

This post was a similar issue if you'd like to review it as well.

like image 158
m_callens Avatar answered Oct 10 '22 09:10

m_callens