Stream type action works with object reputation

Why is the thread throwing an error using an object as a reference here:

type LoadUserData = { type: actionTypes.LOAD_USER_DATA, user: User };

      

But when a string is used, no error occurs:

type LoadUserData = { type: "LOAD_USER_DATA", user: User };

      

The resulting error:

Error:(8, 29) Flow: string. Ineligible value used in/as type annotation (did you forget 'typeof'?) LOAD_USER_DATA

      

0


source to share


2 answers


You can use typeof

as he suggests, but this probably won't do what you want:

const actionTypes = {
  LOAD_USER_DATA: 'foo'
}

type LoadUserData = { type: typeof actionTypes.LOAD_USER_DATA};

({type: 'bar'}: LoadUserData) // no errors

      



This is because Flow indicates the type actionTypes.LOAD_USER_DATA

to string

.

Unfortunately, for your use case, you probably have to just write the string literals again in the type.

+1


source


Unfortunately Flow

does not allow constants in your type definitions. It must be either a data type or a constant value, but not variables. So you'll have to stick with:

type LoadUserData = { type: 'LOAD_USER_DATA', user: User }

      



I am assuming this is the type definition for the action Redux

, in which case the Facebook docs really recommend it anyway!

+1


source







All Articles