aikoven / typescript-fsa

Type-safe action creator utilities
MIT License
607 stars 28 forks source link

Action with unknown payload (TypeScript 3) #65

Closed jollytoad closed 5 years ago

jollytoad commented 6 years ago

I've recently updated to TS3, and although typescript-fsa@3.0.0-beta-2 isn't currently working for me (reported in a separate issue), i'm continuing to use 2.5.0.

I have found that the new unknown type is incredibly useful as the payload type of an Action, especially in combination with redux-observable, and the ofAction operator (see https://github.com/m0a/typescript-fsa-redux-observable/issues/6).

With this introduction of unknown, and some refactoring it's become clear to me that all of my use case of AnyAction should really be Action<unknown>, and I find I now have Action<unknown> appearing all over the place. This is making me think that unknown should be the default payload type, and there really is no place for AnyAction in well typed code.

I'm wondering whether it would be appropriate for Action type declaration to default its payload type param to unknown (obviously backwards compatibly would need to be accounted for).

This may just be due to my use-cases and usage of redux-observable Epics, and the narrowing effect of the ofAction from Action<unknown> to Action<P>. I'm not sure if it would be appropriate for users of thunk, saga, or other non-redux based uses.

aikoven commented 6 years ago

I think it makes a lot of sense to make Payload type parameter default to unknown.

However, AnyAction still makes sense forisType() (and ActionCreator.match(), and ofAction()), because what they do is narrow the widest type there is with a type property to an FSA. Imagine if your app had multiple sources of actions — your app itself dispatching FSAs and some third-party library using its own convention. Then it would be incorrect to say that all actions in your store are Action<unknown>. That's what AnyAction is — the type of any possible action ever.

aikoven commented 5 years ago

I'm going to close this, please feel free to reopen.