Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: a default value for createAction with payload and meta #17

Closed
Jack-Works opened this issue Mar 3, 2018 · 11 comments
Closed

Comments

@Jack-Works
Copy link

Jack-Works commented Mar 3, 2018

There should be a

actionCreator<P, M>(type: ...): (Payload: P, Meta: M) => {type: ..., payload: P, meta: M}
@Jack-Works Jack-Works changed the title suggestion: a default value for createAction suggestion: a default value for createAction with payload and meta Mar 3, 2018
@Jack-Works Jack-Works reopened this Mar 3, 2018
@piotrwitek
Copy link
Owner

Hi,
Please explain what you are trying to achieve, what are the benefits from this and the example usage.

@Jack-Works
Copy link
Author

const aNormalAction = actionCreator("normal")

aNormalAction(2)
// I get {type: "normal", payload: 2}

Benefits here is we can create a simple action without repeating (...) => ({type: ..., payload: ...})

@piotrwitek
Copy link
Owner

Ah ok, so you basically want an escape hatch for a payload with type any, I need to think about it because the goal here is to have a strictly typed experience.
actionCreator without payload is specifically designed to show an error when you try to provide some input parameter and this is a good thing, because it's a TS library focused on limiting the developers errors and using your actions in incorrect way that you designed them in the first place.

@Jack-Works
Copy link
Author

What? No, I don't want "any" either.

I want to provide a type parameter to actionCreator, and then a typed function is return, like this

function actionCreator<Payload, Type extends string>(type: Type, creatorFn: (payload: Payload) => ({ type: Type, payload: Payload }) = payload => ({ type, payload }))

@Jack-Works
Copy link
Author

With this approach, I can get a typed action but without provide a creatorFn (well, this is for simple actions)

@piotrwitek
Copy link
Owner

piotrwitek commented Mar 3, 2018

Ok but what you want to do is technically impossible, if you don't provide a payload type when invoking a higher order function how type inference is supposed to get the payload type?
Have you tested it?
If yes then please show me the reference implementation of actionCreator function only for this specific case, should be easy.

EDIT: Please note that this library is against requiring the consumer to fill any generics type parameters in the API.

@Jack-Works
Copy link
Author

image
Alright, no filling generics type? Then it becomes impossible now

@piotrwitek
Copy link
Owner

@Jack-Works At least IMO there shouldn't be with the current API because that would be breaking change, maybe we could think about adding a new API but it should be generic enough to handle most of possible use cases, because I don't want to end up with a separate creator for each use case.

@piotrwitek
Copy link
Owner

piotrwitek commented Mar 3, 2018

Also I can see that the type string literal will lose it's type (and become any or string) if you do not explicitly provide it as a generic type argument, which is cumbersome.

@Jack-Works
Copy link
Author

Yes... If modify createAction directly to support explicit payload type, type will be any...

@piotrwitek
Copy link
Owner

piotrwitek commented Mar 3, 2018

I'm about to experiment if it would be possible to infer the type literal type just from argument using the newest infer mechanics from conditional types, I guess that it should work, but I need to make sure. That would simplify API and keep it DRY getting rid of that additional generic type argument for type string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants