Closed mwq27 closed 6 years ago
Can you show the code for types.LOGIN_REQUEST
including it's type definition if you wrote one. You don't have to use a raw string - but what you supply has to have a literal string type - not just a string, and there are a few ways that you could end up widening your literal string type, like creating it with concatenation.
Here's my types object:
export const types = {
AUTO_LOGIN: 'AUTH/AUTH_AUTO_LOGIN',
SIGNUP_REQUEST: 'AUTH/SIGNUP_REQUEST',
SIGNUP_SUCCESS: 'AUTH/SIGNUP_SUCCESS',
....
I don't have a typedef for that object.
Do i need to do this:
type MyTypes = {
AUTO_LOGIN: 'AUTH/AUTH_AUTO_LOGIN',
SIGNUP_REQUEST: 'AUTH/SIGNUP_REQUEST',
....
export const types: MyTypes = {
AUTO_LOGIN: 'AUTH/AUTH_AUTO_LOGIN',
SIGNUP_REQUEST: 'AUTH/SIGNUP_REQUEST',
...
Use a string enum. It's complicated but literal types are only used for constant values, and in an object literal the properties are considered mutable. Though your solution works too - just more typing - it does let you use keyof
to create union types though. You could have also said foo: "FOO" as "FOO"
Thank you, I switched to using an enum. Thanks for your help.
I was wondering if this is the intended use.
With the
login
action above, I get this output withreturnsOfActions
:The type for the login_request action returns as
string
, and not the actual value oftypes.LOGIN_REQUEST
. Is there any way I can continue using mytypes
object, or do I need to use the actual value when I usecreateAction
?Thanks