Why does flow throw an error using an object as Reference here:
type LoadUserData = { type: actionTypes.LOAD_USER_DATA, user: User };
But when a string is used no error is thrown:
type LoadUserData = { type: "LOAD_USER_DATA", user: User };
The error that is thrown is:
Error:(8, 29) Flow: string. Ineligible value used in/as type annotation (did you forget 'typeof'?) LOAD_USER_DATA
You can use typeof
as it suggests, but that 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 happens because Flow infers the type of actionTypes.LOAD_USER_DATA
to be string
.
Unfortunately for your use case you will probably have to just write out the string literals again in the type.
Unfortunately, Flow
doesn't allow you to use constants within your type definitions. It must either be a data type or a constant value, but no variables. Therefore, you'll have to stick with the:
type LoadUserData = { type: 'LOAD_USER_DATA', user: User }
I'm assuming this is a type definition for a Redux
action, in which case, Facebook's docs actually recommend this anyway!