Skip to content

Allow Symbol as action name #143

Closed
Closed

Description

@govorov

Hi!
As you suggested, I'm recreating this issue:
ngrx/store#415

When I'm trying to use symbol as action name I receive this error:
TS2678: Type 'symbol' is not comparable to type 'string'.

This error is raised due to this interface definition:
https://github.com/ngrx/store/blob/master/src/dispatcher.ts#L4

export interface Action {
  type: string;
  payload?: any;
}

I think it's reasonable to allow action names to be Symbols, not only strings. It will make possible real encapsulation between modules and avoid any ction name collisions. Now I have to use very long action names to make them unique (some kind of reducerName.actionName or similar). That's really annoying when your project grows. The only thing that might break is user appearance in Redux Dev Tools, as it has action names list and expects them to be strings.
Thank you.

Activity

  1. bfricka commented on Jul 21, 2017

    @bfricka
    Contributor

    So you're just looking for:

    export interface Action {
      type: string | Symbol
    }

    ?

  2. govorov commented on Jul 21, 2017

    @govorov
    Author

    Yes, but I can't say for sure that this is enough and something deep inside the sources would not break.

  3. karptonite commented on Jul 21, 2017

    @karptonite
    Contributor

    If the action type were a symbol, how would that work with the dev tools, which show the actions listed by type? Would it show just the symbol? And if you use symbols to avoid problems with collisions, you will have ambiguities in your devTools action list.

  4. bfricka commented on Jul 23, 2017

    @bfricka
    Contributor

    I don't see that as being a big deal. It should be totally up to users to determine how they want to compose their actions. Personally, I prefer to make it more explicit, but that's just me. Not only that, but it seems like global namespaced symbols should be supported Symbol.for('root.feature.init'). Again, not my cup of tea, but I don't see why we can't support it.

    Am I missing something?

  5. govorov commented on Jul 23, 2017

    @govorov
    Author

    @karptonite, @bfricka Thank you for your response!
    I don't think DevTools limitations should impact on the library API and application design, and my point is that it's a developer who decides whether to use DevTools advantages and String as actions, or use Symbol and make DevTools useless in that case. As no backward compatibility is broken, I think this update will be useful for some users, whereas others will just continue to use strings and see no difference in their workflow. As for DevTools, I can't say for now how to deal with it and Symbol-typed actions. The only idea I have now is to use symbol descriptions and show them in actions list.

  6. kylecordes commented on Jul 23, 2017

    @kylecordes

    The only thing that might break is user appearance in Redux Dev Tools, as it has action names list and expects them to be strings.

    Here is another (big?) think that would break. As I understand, a historical reason to require strings, not symbols, was that the former can take a round trip ride to/from JSON in the trivial manner. This is a very useful property, Actions as "just data". Some systems (including code we have in production...) enjoy and rely on this property.

    Of course, it should be possible to carry by my moving to a more sophisticated serialization library and data format, something that round trips symbols. This is workable, though less convenient, another library to pick out and bundle, and use, etc

    (I just wrote a very similar comment about a move away from "just data" to "must be the right class" in #160 .)

    If a change like this goes to to Store, I would appreciate very much if that included an explanation in the README about the need for more-than-just-JSON for Actions, and a recommendation link or two, or libraries that effectively read/write symbols, classes, etc.

  7. govorov commented on Jul 23, 2017

    @govorov
    Author

    @kylecordes I didn't think about serialization at all. You're right, non-strings will be way too hard to serialize and deserialize. If many of users rely on this functionaity, using strings is a best case. Compared to serialization issues, incapsulation seems to be a minor problem.

  8. bfricka commented on Jul 24, 2017

    @bfricka
    Contributor

    First of all, "just data" is not even implemented in the ngrx library itself. Look at router-store. It uses RouterStateSnapshot and because of this is running in to serialization issues associated with redux-devtools. To me serialization only matters if it's not support by the structured cloning algorithm. In case of Symbols, it's not, so I'd agree that is a decent argument for not using them. This would be problematic, for example, if we wanted to move forward with Worker Effects because structured clone is what is used to pass data to worker threads.

    So in the context of this ticket, for sure, good thoughts on serialization!

    That said, I don't think this is as strong a consideration when it comes to JSON, as a good implementation will define interfaces for (de)serialization (e.g. ngrx-store-localstorage has this). In this case, you can easily rehydrate to the correct types.

    I only mention this, b/c I don't think it's helpful to consider Redux a "POJO-only" sort of pattern. Serialization is a consideration, and in certain cases issues with serialization need to be weighed against the benefits. Because Symbols don't buy you much, I don't think the case is good, so I'm grateful you brought this up.

  9. ShiftySituation commented on Jul 26, 2017

    @ShiftySituation

    Just a tip: If you are looking for type safety and you create classes to represent your actions, you can make your type a static property...

    export class SomeAction implements Action { static get getType(): string { return 'SOME_ACTION'; } readonly type = SomeAction.getType; constructor(public someRequiredValue: string) { } }

    Now you can use SomeAction.getType anywhere you need the type and now there is only one place to change the magic string... but at this point it doesn't feel so magical.

  10. brandonroberts commented on Aug 3, 2017

    @brandonroberts
    Member

    Closing this as serialization is still a priority for actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions