Feature request: Prevent tag name collisions
See original GitHub issueIn order to avoid name collisions for Redux action types it is currently necessary (or at least best practice) to repeat the domain in the type name, for instance:
export const AuthActions = unionize({
AUTH_LOGIN: ofType<LoginData>(),
AUTH_LOGIN_SUCCESS: ofType<AuthData>(),
AUTH_LOGOUT: ofType<void>(),
AUTH_ERROR: ofType<any>(),
}, {
tag: 'type',
value: 'payload',
});
One way to alleviate the issue would be if the type string could be prefixed, for instance:
export const AuthActions = unionize({
LOGIN: ofType<LoginData>(),
LOGIN_SUCCESS: ofType<AuthData>(),
LOGOUT: ofType<void>(),
ERROR: ofType<any>(),
}, {
tag: 'type',
value: 'payload',
tagPrefix: 'AUTH_'
});
Consumers could then reference things by their short names, e.g. AuthActions.LOGIN
while the actual string behind the scenes would be AUTH_LOGIN
, thus avoiding collisions with other non-auth domains.
However, this still wouldn’t guarantee that tag name collisions wouldn’t occur. For that a registry of tags would be necessary as well as a way to configure whether the tag names should be considered unique. The registry could have a structure like
const TAG_REGISTRY: {[tag: string]: string[]} = {
'type': [
'AUTH_LOGIN',
'AUTH_LOGIN_SUCCESS',
'AUTH_LOGOUT',
'AUTH_ERROR',
]
};
and the configuration whether a tag should be considered unique could be done by an option unique: true
or similar:
{
tag: 'type',
value: 'payload',
tagPrefix: 'AUTH_',
unique: true
};
Then if a tag name for the same tag (e.g. type
) were to be used twice, an error could be thrown, thus preventing accidental action type collisions.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:12 (7 by maintainers)
So, I guess now that I think about it, this might be a pretty useful combinator to have on the
unionize
d object itself.I want something like:
Then I can have nice unique tags to read in my redux devtools.